-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat_api.py
30 lines (25 loc) · 977 Bytes
/
chat_api.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
# openai_api.py
import os
import sys
import base64
from openai import OpenAI
def get_openai_completion(input_messages):
send_messages = [{"role": "system", "content": "You are an algorithmic assistant who can solve and explain various algorithmic problems."},
{"role": "user", "content": input_messages}]
api_key = os.environ.get('OPENAI_API_KEY')
if api_key:
client = OpenAI(api_key=api_key)
else:
print("API key not found. Please set the OPENAI_API_KEY environment variable.")
try:
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=send_messages)
return completion.choices[0].message
except Exception as e:
return str(e)
if __name__ == '__main__':
encoded_message = sys.argv[1]
decoded_message = base64.b64decode(encoded_message).decode('utf-8')
output_data = get_openai_completion(decoded_message)
print(output_data.content)