Spaces:
Build error
Build error
Upload OpenAi.py
Browse files- llmservice/OpenAi.py +27 -0
llmservice/OpenAi.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
|
| 3 |
+
class OpenAi:
|
| 4 |
+
def __init__(self, apiKey, options={}, socket=None):
|
| 5 |
+
self.apiKey = apiKey
|
| 6 |
+
self.options = options
|
| 7 |
+
self.socket = socket
|
| 8 |
+
openai.api_key = self.apiKey
|
| 9 |
+
|
| 10 |
+
def ask(self, context, question, functions=[]):
|
| 11 |
+
try:
|
| 12 |
+
response = openai.chat.completions.create(
|
| 13 |
+
model="gpt-4o-mini-2024-07-18",
|
| 14 |
+
messages=context,
|
| 15 |
+
# functions=functions,
|
| 16 |
+
max_tokens=150,
|
| 17 |
+
n=1,
|
| 18 |
+
stop=None,
|
| 19 |
+
temperature=0.7
|
| 20 |
+
)
|
| 21 |
+
return {
|
| 22 |
+
'msg': response.choices[0].message.content,
|
| 23 |
+
'raw': response
|
| 24 |
+
}
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Error in OpenAi.ask: {e}")
|
| 27 |
+
return None
|