Sanchit Verma
commited on
Commit
·
1856369
1
Parent(s):
1b460ab
- Add Gradio-based chat interface in `app.py` for persona-based chat assistant.
Browse files- Update `config.py` to include new configuration options for Ollama and API keys.
- Add `requirements.txt` to specify necessary Python packages.
- Enhance `utils.py` with detailed docstrings and improved function structure.
app.py
CHANGED
@@ -1,2 +1,26 @@
|
|
1 |
-
|
|
|
|
|
|
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from prompts import PERSONAS
|
3 |
+
from utils import generate_response
|
4 |
+
from config import DEFAULT_PERSONA
|
5 |
|
6 |
+
|
7 |
+
def chat_fn(persona, user_input, history):
|
8 |
+
return generate_response(PERSONAS[persona], user_input, history)
|
9 |
+
|
10 |
+
|
11 |
+
with gr.Blocks() as app:
|
12 |
+
gr.Markdown("## 🤖 LLMates: Persona-based Chat Assistant")
|
13 |
+
|
14 |
+
persona = gr.Dropdown(
|
15 |
+
choices=list(PERSONAS.keys()), value=DEFAULT_PERSONA, label="Choose Persona"
|
16 |
+
)
|
17 |
+
chatbox = gr.Chatbot(label="LLMates")
|
18 |
+
msg = gr.Textbox(label="Type your message...")
|
19 |
+
state = gr.State([])
|
20 |
+
|
21 |
+
def user_submit(user_input, history):
|
22 |
+
return chat_fn(persona.value, user_input, history)
|
23 |
+
|
24 |
+
msg.submit(user_submit, [msg, state], [chatbox, state])
|
25 |
+
|
26 |
+
app.launch()
|
config.py
CHANGED
@@ -1,13 +1,15 @@
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
|
4 |
-
load_dotenv()
|
5 |
|
6 |
-
#
|
7 |
-
MODEL_NAME = os.getenv("OPENAI_MODEL", "gpt-4o")
|
8 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
DEFAULT_PERSONA = "Python Tutor"
|
12 |
-
MAX_HISTORY_TURNS = 10 # truncate if needed
|
13 |
TEMPERATURE = 0.7
|
|
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
|
4 |
+
load_dotenv()
|
5 |
|
6 |
+
# Model and key
|
|
|
7 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
8 |
+
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o")
|
9 |
+
USE_OLLAMA = os.getenv("USE_OLLAMA", "false").lower() == "true"
|
10 |
+
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "llama3")
|
11 |
|
12 |
+
# UI + LLM behavior config
|
13 |
DEFAULT_PERSONA = "Python Tutor"
|
|
|
14 |
TEMPERATURE = 0.7
|
15 |
+
MAX_TURNS = 10
|
requirements.txt
CHANGED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
gradio
|
3 |
+
python-dotenv
|
4 |
+
requests
|
utils.py
CHANGED
@@ -1,31 +1,60 @@
|
|
1 |
from config import OPENAI_API_KEY, OPENAI_MODEL, USE_OLLAMA, OLLAMA_MODEL
|
2 |
import requests
|
3 |
import openai
|
4 |
-
import os
|
5 |
|
6 |
openai.api_key = OPENAI_API_KEY
|
7 |
|
|
|
8 |
def query_openai(messages):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
try:
|
10 |
-
response = openai.ChatCompletion.create(
|
11 |
-
model=OPENAI_MODEL,
|
12 |
-
messages=messages
|
13 |
-
)
|
14 |
return response["choices"][0]["message"]["content"]
|
15 |
except Exception as e:
|
16 |
return f"⚠️ OpenAI Error: {e}"
|
17 |
|
|
|
18 |
def query_ollama(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
try:
|
20 |
res = requests.post(
|
21 |
"http://localhost:11434/api/generate",
|
22 |
-
json={"model": OLLAMA_MODEL, "prompt": prompt}
|
23 |
)
|
24 |
return res.json()["response"]
|
25 |
except Exception as e:
|
26 |
return f"⚠️ Ollama Error: {e}"
|
27 |
|
|
|
28 |
def generate_response(persona, user_input, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
if USE_OLLAMA:
|
30 |
full_prompt = f"{persona}\n\n"
|
31 |
for u, b in history:
|
@@ -41,4 +70,4 @@ def generate_response(persona, user_input, history):
|
|
41 |
reply = query_openai(messages)
|
42 |
|
43 |
history.append((user_input, reply))
|
44 |
-
return history, history
|
|
|
1 |
from config import OPENAI_API_KEY, OPENAI_MODEL, USE_OLLAMA, OLLAMA_MODEL
|
2 |
import requests
|
3 |
import openai
|
|
|
4 |
|
5 |
openai.api_key = OPENAI_API_KEY
|
6 |
|
7 |
+
|
8 |
def query_openai(messages):
|
9 |
+
"""Query the OpenAI API with the given messages.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
messages (list): A list of message dictionaries, where each dictionary contains
|
13 |
+
'role' and 'content' keys representing the conversation history.
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
str: The assistant's response as a string, or an error message if the API call fails.
|
17 |
+
"""
|
18 |
try:
|
19 |
+
response = openai.ChatCompletion.create(model=OPENAI_MODEL, messages=messages)
|
|
|
|
|
|
|
20 |
return response["choices"][0]["message"]["content"]
|
21 |
except Exception as e:
|
22 |
return f"⚠️ OpenAI Error: {e}"
|
23 |
|
24 |
+
|
25 |
def query_ollama(prompt):
|
26 |
+
"""Query a local Ollama instance with the given prompt.
|
27 |
+
|
28 |
+
Args:
|
29 |
+
prompt (str): The input prompt to send to the Ollama model.
|
30 |
+
|
31 |
+
Returns:
|
32 |
+
str: The model's response as a string, or an error message if the API call fails.
|
33 |
+
"""
|
34 |
try:
|
35 |
res = requests.post(
|
36 |
"http://localhost:11434/api/generate",
|
37 |
+
json={"model": OLLAMA_MODEL, "prompt": prompt},
|
38 |
)
|
39 |
return res.json()["response"]
|
40 |
except Exception as e:
|
41 |
return f"⚠️ Ollama Error: {e}"
|
42 |
|
43 |
+
|
44 |
def generate_response(persona, user_input, history):
|
45 |
+
"""Generate a response using either OpenAI or Ollama based on configuration.
|
46 |
+
|
47 |
+
Args:
|
48 |
+
persona (str): The system prompt or persona that defines the assistant's behavior.
|
49 |
+
user_input (str): The latest user input message.
|
50 |
+
history (list): A list of tuples representing the conversation history,
|
51 |
+
where each tuple is (user_message, bot_response).
|
52 |
+
|
53 |
+
Returns:
|
54 |
+
tuple: A tuple containing:
|
55 |
+
- Updated conversation history including the new exchange
|
56 |
+
- The same history (for compatibility with some interfaces)
|
57 |
+
"""
|
58 |
if USE_OLLAMA:
|
59 |
full_prompt = f"{persona}\n\n"
|
60 |
for u, b in history:
|
|
|
70 |
reply = query_openai(messages)
|
71 |
|
72 |
history.append((user_input, reply))
|
73 |
+
return history, history
|