-
Notifications
You must be signed in to change notification settings - Fork 2
/
oai.py
30 lines (25 loc) · 678 Bytes
/
oai.py
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
import os
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
# init client and connect to localhost server
client = OpenAI(
api_key=OPENAI_API_KEY,
base_url="http://localhost:5001", # change the default port if needed
)
# call API
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": "Say this is a test",
}
],
model="Meta-Llama-3.1-8B-Instruct",
)
# print the top "choice"
if chat_completion.choices:
print(chat_completion.choices[0].message.content)
else:
print("Error: No completion returned")