Spaces:
Build error
Build error
Create openai_api.py
Browse files- openai_api.py +29 -0
openai_api.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
|
| 3 |
+
# Set your OpenAI API key
|
| 4 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 5 |
+
|
| 6 |
+
COMPLETIONS_MODEL = "gpt-4o-mini"# "gpt-3.5-turbo-instruct" used earlier .
|
| 7 |
+
EMBEDDING_MODEL = "text-embedding-3-large" # "text-embedding-3-small"
|
| 8 |
+
|
| 9 |
+
META_PROMPT="You are an AI assistant for the IT Helpdesk at Harvey Mudd College."
|
| 10 |
+
|
| 11 |
+
def get_embedding(text: str, model: str=EMBEDDING_MODEL) -> list[float]:
|
| 12 |
+
result = openai.Embedding.create(
|
| 13 |
+
model=model,
|
| 14 |
+
input=text
|
| 15 |
+
)
|
| 16 |
+
return result["data"][0]["embedding"]
|
| 17 |
+
|
| 18 |
+
def get_response(prompt: str, model: str=COMPLETIONS_MODEL) -> list[float]:
|
| 19 |
+
"""Chat completion using OpenAI's GPT models."""
|
| 20 |
+
response = openai.ChatCompletion.create(
|
| 21 |
+
model = COMPLETIONS_MODEL,
|
| 22 |
+
messages=[
|
| 23 |
+
{"role": "system", "content": META_PROMPT},
|
| 24 |
+
{"role": "user", "content": prompt}
|
| 25 |
+
],
|
| 26 |
+
temperature=0,
|
| 27 |
+
max_completion_tokens=800
|
| 28 |
+
)
|
| 29 |
+
return response["choices"][0]["message"]["content"]
|