Spaces:
Runtime error
Runtime error
:gem: [Feature] Working example with openai-python (ToFix: NO OUTPUT)
Browse files- examples/chat_with_openai.py +38 -0
examples/chat_with_openai.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
|
3 |
+
base_url = "http://localhost:22222"
|
4 |
+
api_key = "sk-xxxxx"
|
5 |
+
|
6 |
+
|
7 |
+
client = OpenAI(base_url=base_url, api_key=api_key)
|
8 |
+
|
9 |
+
extra_body = {
|
10 |
+
"invocation_id": 1,
|
11 |
+
}
|
12 |
+
|
13 |
+
response = client.chat.completions.create(
|
14 |
+
model="precise",
|
15 |
+
messages=[
|
16 |
+
{
|
17 |
+
"role": "user",
|
18 |
+
"content": "how many questions have I asked you?",
|
19 |
+
}
|
20 |
+
],
|
21 |
+
stream=True,
|
22 |
+
extra_body=extra_body,
|
23 |
+
)
|
24 |
+
|
25 |
+
print(response)
|
26 |
+
|
27 |
+
for chunk in response:
|
28 |
+
# print(chunk.choices[0].delta)
|
29 |
+
print("??")
|
30 |
+
|
31 |
+
# print(response.choices[0].message)
|
32 |
+
# print(response)
|
33 |
+
|
34 |
+
# for chunk in stream:
|
35 |
+
# if chunk.choices[0].delta.content is not None:
|
36 |
+
# print(chunk.choices[0].delta.content)
|
37 |
+
# else:
|
38 |
+
# print(chunk)
|