rentbot / utils.py
mgbam's picture
Update utils.py
b9e5a14 verified
import os
import openai
from dotenv import load_dotenv
def init_openai() -> None:
"""
Load environment variables and initialize OpenAI API key.
"""
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY not found in environment")
openai.api_key = api_key
def transcribe_audio(file_path: str) -> str:
"""
Transcribe an audio file using OpenAI Whisper API.
Returns the transcribed text.
"""
with open(file_path, "rb") as audio_file:
response = openai.Audio.transcribe(
model="whisper-1",
file=audio_file
)
return response.get("text", "")
def chat_with_gpt(system_prompt: str, user_message: str) -> str:
"""
Send a chat message to OpenAI GPT and return the assistant reply.
"""
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_message}
]
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=messages,
temperature=0.7,
stream=False
)
return response.choices[0].message.content