File size: 1,165 Bytes
5bc0a64
 
 
 
6f740bf
 
 
 
 
5bc0a64
6f740bf
 
 
 
 
5bc0a64
6f740bf
5bc0a64
 
6f740bf
5bc0a64
 
 
 
 
 
 
 
6f740bf
 
5bc0a64
6f740bf
5bc0a64
 
 
 
 
 
 
 
6f740bf
 
5bc0a64
b9e5a14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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