Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,15 +1,23 @@
|
|
| 1 |
-
|
| 2 |
import os
|
| 3 |
import openai
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
load_dotenv()
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
"""
|
| 12 |
Transcribe an audio file using OpenAI Whisper API.
|
|
|
|
| 13 |
"""
|
| 14 |
with open(file_path, "rb") as audio_file:
|
| 15 |
response = openai.Audio.transcribe(
|
|
@@ -18,9 +26,10 @@ async def transcribe_audio(file_path: str) -> str:
|
|
| 18 |
)
|
| 19 |
return response.get("text", "")
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
"""
|
| 23 |
-
Send a chat message to OpenAI and return the assistant reply.
|
| 24 |
"""
|
| 25 |
messages = [
|
| 26 |
{"role": "system", "content": system_prompt},
|
|
@@ -29,6 +38,7 @@ async def chat_with_gpt(system_prompt: str, user_message: str) -> str:
|
|
| 29 |
response = openai.ChatCompletion.create(
|
| 30 |
model="gpt-4o-mini",
|
| 31 |
messages=messages,
|
| 32 |
-
temperature=0.7
|
|
|
|
| 33 |
)
|
| 34 |
-
return response.choices[0].message.content
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import openai
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
|
| 5 |
+
|
| 6 |
+
def init_openai() -> None:
|
| 7 |
+
"""
|
| 8 |
+
Load environment variables and initialize OpenAI API key.
|
| 9 |
+
"""
|
| 10 |
load_dotenv()
|
| 11 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 12 |
+
if not api_key:
|
| 13 |
+
raise ValueError("OPENAI_API_KEY not found in environment")
|
| 14 |
+
openai.api_key = api_key
|
| 15 |
+
|
| 16 |
|
| 17 |
+
def transcribe_audio(file_path: str) -> str:
|
| 18 |
"""
|
| 19 |
Transcribe an audio file using OpenAI Whisper API.
|
| 20 |
+
Returns the transcribed text.
|
| 21 |
"""
|
| 22 |
with open(file_path, "rb") as audio_file:
|
| 23 |
response = openai.Audio.transcribe(
|
|
|
|
| 26 |
)
|
| 27 |
return response.get("text", "")
|
| 28 |
|
| 29 |
+
|
| 30 |
+
def chat_with_gpt(system_prompt: str, user_message: str) -> str:
|
| 31 |
"""
|
| 32 |
+
Send a chat message to OpenAI GPT and return the assistant reply.
|
| 33 |
"""
|
| 34 |
messages = [
|
| 35 |
{"role": "system", "content": system_prompt},
|
|
|
|
| 38 |
response = openai.ChatCompletion.create(
|
| 39 |
model="gpt-4o-mini",
|
| 40 |
messages=messages,
|
| 41 |
+
temperature=0.7,
|
| 42 |
+
stream=False
|
| 43 |
)
|
| 44 |
+
return response.choices[0].message.content
|