openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.2.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/beta/test_threads.py

318lines · modeblame

baa9f07fRobert Craigie2 years ago1# File generated from our OpenAPI spec by Stainless.
2
3from __future__ import annotations
4
5import os
6
7import pytest
8
9from openai import OpenAI, AsyncOpenAI
10from tests.utils import assert_matches_type
11from openai._client import OpenAI, AsyncOpenAI
12from openai.types.beta import Thread, ThreadDeleted
13from openai.types.beta.threads import Run
14
15base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
16api_key = "My API Key"
17
18
19class TestThreads:
20strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
21loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
22parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
23
24@parametrize
25def test_method_create(self, client: OpenAI) -> None:
26thread = client.beta.threads.create()
27assert_matches_type(Thread, thread, path=["response"])
28
29@parametrize
30def test_method_create_with_all_params(self, client: OpenAI) -> None:
31thread = client.beta.threads.create(
32messages=[
33{
34"role": "user",
35"content": "x",
36"file_ids": ["string"],
37"metadata": {},
38},
39{
40"role": "user",
41"content": "x",
42"file_ids": ["string"],
43"metadata": {},
44},
45{
46"role": "user",
47"content": "x",
48"file_ids": ["string"],
49"metadata": {},
50},
51],
52metadata={},
53)
54assert_matches_type(Thread, thread, path=["response"])
55
56@parametrize
57def test_raw_response_create(self, client: OpenAI) -> None:
58response = client.beta.threads.with_raw_response.create()
59assert response.http_request.headers.get("X-Stainless-Lang") == "python"
60thread = response.parse()
61assert_matches_type(Thread, thread, path=["response"])
62
63@parametrize
64def test_method_retrieve(self, client: OpenAI) -> None:
65thread = client.beta.threads.retrieve(
66"string",
67)
68assert_matches_type(Thread, thread, path=["response"])
69
70@parametrize
71def test_raw_response_retrieve(self, client: OpenAI) -> None:
72response = client.beta.threads.with_raw_response.retrieve(
73"string",
74)
75assert response.http_request.headers.get("X-Stainless-Lang") == "python"
76thread = response.parse()
77assert_matches_type(Thread, thread, path=["response"])
78
79@parametrize
80def test_method_update(self, client: OpenAI) -> None:
81thread = client.beta.threads.update(
82"string",
83)
84assert_matches_type(Thread, thread, path=["response"])
85
86@parametrize
87def test_method_update_with_all_params(self, client: OpenAI) -> None:
88thread = client.beta.threads.update(
89"string",
90metadata={},
91)
92assert_matches_type(Thread, thread, path=["response"])
93
94@parametrize
95def test_raw_response_update(self, client: OpenAI) -> None:
96response = client.beta.threads.with_raw_response.update(
97"string",
98)
99assert response.http_request.headers.get("X-Stainless-Lang") == "python"
100thread = response.parse()
101assert_matches_type(Thread, thread, path=["response"])
102
103@parametrize
104def test_method_delete(self, client: OpenAI) -> None:
105thread = client.beta.threads.delete(
106"string",
107)
108assert_matches_type(ThreadDeleted, thread, path=["response"])
109
110@parametrize
111def test_raw_response_delete(self, client: OpenAI) -> None:
112response = client.beta.threads.with_raw_response.delete(
113"string",
114)
115assert response.http_request.headers.get("X-Stainless-Lang") == "python"
116thread = response.parse()
117assert_matches_type(ThreadDeleted, thread, path=["response"])
118
119@parametrize
120def test_method_create_and_run(self, client: OpenAI) -> None:
121thread = client.beta.threads.create_and_run(
122assistant_id="string",
123)
124assert_matches_type(Run, thread, path=["response"])
125
126@parametrize
127def test_method_create_and_run_with_all_params(self, client: OpenAI) -> None:
128thread = client.beta.threads.create_and_run(
129assistant_id="string",
130instructions="string",
131metadata={},
132model="string",
133thread={
134"messages": [
135{
136"role": "user",
137"content": "x",
138"file_ids": ["string"],
139"metadata": {},
140},
141{
142"role": "user",
143"content": "x",
144"file_ids": ["string"],
145"metadata": {},
146},
147{
148"role": "user",
149"content": "x",
150"file_ids": ["string"],
151"metadata": {},
152},
153],
154"metadata": {},
155},
156tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
157)
158assert_matches_type(Run, thread, path=["response"])
159
160@parametrize
161def test_raw_response_create_and_run(self, client: OpenAI) -> None:
162response = client.beta.threads.with_raw_response.create_and_run(
163assistant_id="string",
164)
165assert response.http_request.headers.get("X-Stainless-Lang") == "python"
166thread = response.parse()
167assert_matches_type(Run, thread, path=["response"])
168
169
170class TestAsyncThreads:
171strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
172loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
173parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
174
175@parametrize
176async def test_method_create(self, client: AsyncOpenAI) -> None:
177thread = await client.beta.threads.create()
178assert_matches_type(Thread, thread, path=["response"])
179
180@parametrize
181async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None:
182thread = await client.beta.threads.create(
183messages=[
184{
185"role": "user",
186"content": "x",
187"file_ids": ["string"],
188"metadata": {},
189},
190{
191"role": "user",
192"content": "x",
193"file_ids": ["string"],
194"metadata": {},
195},
196{
197"role": "user",
198"content": "x",
199"file_ids": ["string"],
200"metadata": {},
201},
202],
203metadata={},
204)
205assert_matches_type(Thread, thread, path=["response"])
206
207@parametrize
208async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
209response = await client.beta.threads.with_raw_response.create()
210assert response.http_request.headers.get("X-Stainless-Lang") == "python"
211thread = response.parse()
212assert_matches_type(Thread, thread, path=["response"])
213
214@parametrize
215async def test_method_retrieve(self, client: AsyncOpenAI) -> None:
216thread = await client.beta.threads.retrieve(
217"string",
218)
219assert_matches_type(Thread, thread, path=["response"])
220
221@parametrize
222async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None:
223response = await client.beta.threads.with_raw_response.retrieve(
224"string",
225)
226assert response.http_request.headers.get("X-Stainless-Lang") == "python"
227thread = response.parse()
228assert_matches_type(Thread, thread, path=["response"])
229
230@parametrize
231async def test_method_update(self, client: AsyncOpenAI) -> None:
232thread = await client.beta.threads.update(
233"string",
234)
235assert_matches_type(Thread, thread, path=["response"])
236
237@parametrize
238async def test_method_update_with_all_params(self, client: AsyncOpenAI) -> None:
239thread = await client.beta.threads.update(
240"string",
241metadata={},
242)
243assert_matches_type(Thread, thread, path=["response"])
244
245@parametrize
246async def test_raw_response_update(self, client: AsyncOpenAI) -> None:
247response = await client.beta.threads.with_raw_response.update(
248"string",
249)
250assert response.http_request.headers.get("X-Stainless-Lang") == "python"
251thread = response.parse()
252assert_matches_type(Thread, thread, path=["response"])
253
254@parametrize
255async def test_method_delete(self, client: AsyncOpenAI) -> None:
256thread = await client.beta.threads.delete(
257"string",
258)
259assert_matches_type(ThreadDeleted, thread, path=["response"])
260
261@parametrize
262async def test_raw_response_delete(self, client: AsyncOpenAI) -> None:
263response = await client.beta.threads.with_raw_response.delete(
264"string",
265)
266assert response.http_request.headers.get("X-Stainless-Lang") == "python"
267thread = response.parse()
268assert_matches_type(ThreadDeleted, thread, path=["response"])
269
270@parametrize
271async def test_method_create_and_run(self, client: AsyncOpenAI) -> None:
272thread = await client.beta.threads.create_and_run(
273assistant_id="string",
274)
275assert_matches_type(Run, thread, path=["response"])
276
277@parametrize
278async def test_method_create_and_run_with_all_params(self, client: AsyncOpenAI) -> None:
279thread = await client.beta.threads.create_and_run(
280assistant_id="string",
281instructions="string",
282metadata={},
283model="string",
284thread={
285"messages": [
286{
287"role": "user",
288"content": "x",
289"file_ids": ["string"],
290"metadata": {},
291},
292{
293"role": "user",
294"content": "x",
295"file_ids": ["string"],
296"metadata": {},
297},
298{
299"role": "user",
300"content": "x",
301"file_ids": ["string"],
302"metadata": {},
303},
304],
305"metadata": {},
306},
307tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
308)
309assert_matches_type(Run, thread, path=["response"])
310
311@parametrize
312async def test_raw_response_create_and_run(self, client: AsyncOpenAI) -> None:
313response = await client.beta.threads.with_raw_response.create_and_run(
314assistant_id="string",
315)
316assert response.http_request.headers.get("X-Stainless-Lang") == "python"
317thread = response.parse()
318assert_matches_type(Run, thread, path=["response"])