-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathinput-openaisdk.py
More file actions
61 lines (44 loc) · 1.71 KB
/
Copy pathinput-openaisdk.py
File metadata and controls
61 lines (44 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from openai import OpenAI
import os
from dotenv import load_dotenv
load_dotenv()
history = []
while True:
user_input = input("Your prompt or questions: ")
if user_input.lower() == "exit":
print("Exiting the program.")
break
history.append({"role": "user", "content": user_input})
api_url = os.getenv("OPENAI_API_BASE")
token = os.getenv('OPENAI_API_KEY')
model = os.getenv("OPENAI_MODEL")
client = OpenAI(api_key=token, base_url=api_url)
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful, friendly assistant."},
] + history
)
try:
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful, friendly assistant."},
] + history
)
content = response.choices[0].message.content
print("\nExtracted content:")
print(content)
history.append({"role": "assistant", "content": content})
except Exception as e:
print(f"Error occurred: {e}")
# Your prompt or questions: tell me a short Indian joke
# Extracted content:
# Sure! Here’s a light-hearted Indian joke:
# Why did the math book look sad?
# Because it had too many “problems”… and its family wanted it to become an engineer!
# Your prompt or questions: Make it in the Belgian style
# Extracted content:
# Of course! Here’s the joke with a Belgian touch:
# Pourquoi le livre de maths belge avait-il l’air triste ?
# Parce qu’il avait trop de « problèmes »… et ses parents voulaient quand même qu’il devienne ingénieur !