File size: 1,528 Bytes
82ce420
 
 
14d0f97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82ce420
 
53fbe3a
82ce420
14d0f97
9ad34ed
 
82ce420
14d0f97
 
82ce420
fc2a31e
82ce420
 
 
14d0f97
 
 
 
 
 
 
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
45
46
47
48
import openai
import streamlit as st

class FineTunedChatbot:
    def __init__(self, api_key, model_name):
        openai.api_key = api_key
        self.client = openai
        self.model_name = model_name

    def get_response(self, query_text):
        try:
            # Use client.chat.completions.create
            response = self.client.chat.completions.create(
                model=self.model_name,
                messages=[
                    {"role": "system", "content": "You are a German chatbot. You should help the user by answering their question."},
                    {"role": "user", "content": query_text}
                ]
            )
            # Extract the response content
            answer = response.choices[0].message.content
            return {'answer': answer}

        except Exception as e:
            return {'error': f'Error processing query: {str(e)}'}

# Fine-tuned model name
MODEL_NAME = "ft:gpt-4o-mini-2024-07-18:brenin::AkWzHofJ"

# Streamlit app UI
st.title("Chat with Fine-Tuned GPT")
st.markdown("This chatbot uses a fine-tuned GPT model.")

# Initialize chatbot
chatbot = FineTunedChatbot(api_key=st.secrets["OPENAI_API_KEY"], model_name=MODEL_NAME)

# User input field
user_input = st.text_input("Enter your question:")

if user_input:
    # Fetch response from chatbot
    response = chatbot.get_response(user_input)

    if 'answer' in response:
        st.markdown(f"**Assistant:** {response['answer']}")
    else:
        st.markdown(f"**Error:** {response['error']}")