-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_completion.py
More file actions
29 lines (21 loc) · 864 Bytes
/
openai_completion.py
File metadata and controls
29 lines (21 loc) · 864 Bytes
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
# Python template to demonstrate API call to OpenAI models using the chat.completions.create()
from openai import OpenAI
base_url = "http://localhost:1234/v1/"
system_prompt = "You are a learning assistant and teacher. Respond with accurate answers. If you don't know the answer, then be honest."
user_prompt = "Help me learn how to use AI. Provide a learning outline."
api = OpenAI(base_url=base_url, api_key="none")
def main():
completion = api.chat.completions.create(
model="gemma-3-4b-it",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
temperature=0.7,
max_tokens=256,
)
response = completion.choices[0].message.content
print("User:", user_prompt)
print("AI:", response)
if __name__ == "__main__":
main()