Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,108 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
9 |
|
10 |
-
def
|
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 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
gr.Slider(
|
53 |
-
minimum=0.
|
54 |
-
maximum=
|
55 |
-
value=0.
|
56 |
-
step=0.
|
57 |
-
label="
|
58 |
),
|
59 |
],
|
|
|
|
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
from openai import OpenAI
|
5 |
+
from dotenv import load_dotenv
|
6 |
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
|
|
|
|
9 |
|
10 |
+
# Initialize OpenAI client
|
11 |
+
openai_client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
|
12 |
+
ragie_api_key = os.getenv('RAGIE_API_KEY')
|
13 |
|
14 |
+
async def get_ragie_chunks(query):
|
15 |
+
"""Retrieve chunks using Ragie's documented method"""
|
16 |
+
import aiohttp
|
17 |
+
|
18 |
+
async with aiohttp.ClientSession() as session:
|
19 |
+
async with session.post(
|
20 |
+
"https://api.ragie.ai/retrievals",
|
21 |
+
headers={
|
22 |
+
"Content-Type": "application/json",
|
23 |
+
"Authorization": f"Bearer {ragie_api_key}"
|
24 |
+
},
|
25 |
+
json={
|
26 |
+
"query": query,
|
27 |
+
"filter": {
|
28 |
+
"scope": "tutorial" # Adjust this to match your Ragie scope
|
29 |
+
}
|
30 |
+
}
|
31 |
+
) as response:
|
32 |
+
if response.status != 200:
|
33 |
+
return []
|
34 |
+
data = await response.json()
|
35 |
+
return [chunk["text"] for chunk in data.get("scored_chunks", [])]
|
36 |
|
37 |
+
def create_system_prompt(chunks):
|
38 |
+
"""Create system prompt following Ragie's format"""
|
39 |
+
return f"""These are very important to follow:
|
40 |
+
You are "Ragie AI", a professional but friendly AI chatbot working as an assistant.
|
41 |
+
Your current task is to help the user based on all of the information available to you.
|
42 |
+
Answer informally, directly, and concisely without a heading or greeting but include details.
|
43 |
+
Use richtext Markdown when appropriate including bold, italic, paragraphs, and lists.
|
44 |
+
If using LaTeX, use double $$ as delimiter instead of single $. Use $$....$$ instead of $..$$.
|
45 |
+
Organize information into multiple sections or points when appropriate.
|
46 |
+
Don't include raw item IDs or other raw fields from the source.
|
47 |
+
Don't use XML or other markup unless requested by the user.
|
48 |
|
49 |
+
Here is all of the information available to answer the user:
|
50 |
+
===
|
51 |
+
{chr(10).join(chunks)}
|
52 |
+
===
|
53 |
|
54 |
+
If the user asked for a search and there are no results, make sure to let the user know
|
55 |
+
and what they might be able to do to find the information they need."""
|
56 |
|
57 |
+
async def respond(message, history, temperature):
|
58 |
+
"""Main response function following Ragie's integration approach"""
|
59 |
+
try:
|
60 |
+
# Get chunks from Ragie using their method
|
61 |
+
chunks = await get_ragie_chunks(message)
|
62 |
+
|
63 |
+
# Create messages array following their format
|
64 |
+
messages = [{"role": "system", "content": create_system_prompt(chunks)}]
|
65 |
+
|
66 |
+
# Add conversation history
|
67 |
+
for human, assistant in history:
|
68 |
+
messages.append({"role": "user", "content": human})
|
69 |
+
messages.append({"role": "assistant", "content": assistant})
|
70 |
+
|
71 |
+
# Add current message
|
72 |
+
messages.append({"role": "user", "content": message})
|
73 |
+
|
74 |
+
# Get streaming response from OpenAI
|
75 |
+
response = ""
|
76 |
+
stream = openai_client.chat.completions.create(
|
77 |
+
model="gpt-3.5-turbo",
|
78 |
+
messages=messages,
|
79 |
+
temperature=temperature,
|
80 |
+
stream=True
|
81 |
+
)
|
82 |
+
|
83 |
+
for chunk in stream:
|
84 |
+
if chunk.choices[0].delta.content is not None:
|
85 |
+
response += chunk.choices[0].delta.content
|
86 |
+
yield response
|
87 |
+
|
88 |
+
except Exception as e:
|
89 |
+
yield f"I apologize, but I encountered an error: {str(e)}"
|
90 |
|
91 |
+
# Create the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
demo = gr.ChatInterface(
|
93 |
respond,
|
94 |
additional_inputs=[
|
|
|
|
|
|
|
95 |
gr.Slider(
|
96 |
+
minimum=0.0,
|
97 |
+
maximum=2.0,
|
98 |
+
value=0.7,
|
99 |
+
step=0.1,
|
100 |
+
label="Temperature"
|
101 |
),
|
102 |
],
|
103 |
+
title="Ragie-Powered Chatbot",
|
104 |
+
description="A chatbot that combines Ragie's retrieval system with OpenAI's language capabilities."
|
105 |
)
|
106 |
|
|
|
107 |
if __name__ == "__main__":
|
108 |
+
demo.launch()
|