File size: 1,465 Bytes
e171166
189c7f2
 
 
5b0a5a9
189c7f2
 
 
 
 
 
5b0a5a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189c7f2
 
 
e171166
189c7f2
e171166
189c7f2
 
 
 
e171166
 
5b0a5a9
 
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 gradio as gr
from openai import OpenAI
from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

def chat_with_llm(message, history, temperature=0.7, top_p=1.0, max_tokens=256):
    """Function to interact with OpenAI's Chat API with additional controls."""
    
    # Convert history (list of tuples) into OpenAI's expected format
    formatted_history = []
    for user_msg, assistant_msg in history:
        formatted_history.append({"role": "user", "content": user_msg})
        formatted_history.append({"role": "assistant", "content": assistant_msg})

    # Make the API call
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "system", "content": "You are a helpful assistant."}] +
                 formatted_history +  
                 [{"role": "user", "content": message}],
        temperature=temperature,
        top_p=top_p,
        max_tokens=max_tokens
    )
    
    return response.choices[0].message.content

# Create Gradio chat interface with sliders for temperature, top_p, and max_tokens
demo = gr.ChatInterface(
    fn=chat_with_llm,
    additional_inputs=[
        gr.Slider(0, 1, value=0.7, label="Temperature"),
        gr.Slider(0, 1, value=1.0, label="Top-p"),
        gr.Slider(1, 1024, value=256, label="Max Tokens")
    ]
)

# Enable sharing for external access
demo.launch()