Spaces:
Sleeping
Sleeping
Upload openai_client.py
Browse files- llms/openai_client.py +26 -0
llms/openai_client.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import json
|
3 |
+
from typing import List, Dict
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Set your OpenAI API key in an environment variable
|
7 |
+
# This small function is set separately as in an enterprise, invoking api might not be as straightforward.
|
8 |
+
# This would abstract the underlying hops/complexities if we want to make an LLM call.
|
9 |
+
|
10 |
+
|
11 |
+
openai.api_key = os.environ.get('api_key')
|
12 |
+
|
13 |
+
def invoke_api(system_prompt,user_message,temp,max_tokens=50):
|
14 |
+
response = openai.ChatCompletion.create(
|
15 |
+
model="gpt-4o-mini",
|
16 |
+
messages=[
|
17 |
+
{"role": "system", "content": system_prompt},
|
18 |
+
{"role": "user", "content": user_message}
|
19 |
+
],
|
20 |
+
temperature=temp,
|
21 |
+
max_tokens =max_tokens
|
22 |
+
)
|
23 |
+
|
24 |
+
return response
|
25 |
+
|
26 |
+
|