gerasdf
commited on
Commit
·
cf5e123
1
Parent(s):
5572989
first v
Browse files- .gitignore +1 -0
- README.md +2 -2
- app.py +0 -63
- query.py +144 -0
- requirements.txt +1 -1
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
books
|
README.md
CHANGED
@@ -4,9 +4,9 @@ emoji: 💬
|
|
4 |
colorFrom: yellow
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
-
app_file:
|
8 |
pinned: false
|
9 |
license: mit
|
10 |
---
|
11 |
|
12 |
-
An example chatbot
|
|
|
4 |
colorFrom: yellow
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
+
app_file: query.py
|
8 |
pinned: false
|
9 |
license: mit
|
10 |
---
|
11 |
|
12 |
+
An example chatbot doing RAG to fetch context form documents using Astra DB
|
app.py
DELETED
@@ -1,63 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
-
demo = gr.ChatInterface(
|
46 |
-
respond,
|
47 |
-
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
-
|
61 |
-
|
62 |
-
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
query.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from langchain_astradb import AstraDBVectorStore
|
4 |
+
|
5 |
+
from langchain_core.prompts import ChatPromptTemplate
|
6 |
+
from langchain_core.output_parsers import StrOutputParser
|
7 |
+
from langchain_core.runnables import RunnablePassthrough, RunnableLambda
|
8 |
+
from langchain_core.messages import SystemMessage, AIMessage, HumanMessage
|
9 |
+
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
10 |
+
|
11 |
+
import os
|
12 |
+
|
13 |
+
prompt_template = os.environ.get("PROMPT_TEMPLATE")
|
14 |
+
|
15 |
+
prompt = ChatPromptTemplate.from_messages([('system', prompt_template)])
|
16 |
+
|
17 |
+
AI = False
|
18 |
+
|
19 |
+
def ai_setup():
|
20 |
+
global llm, prompt_chain
|
21 |
+
llm = ChatOpenAI(model = "gpt-4o", temperature=0.8)
|
22 |
+
|
23 |
+
if AI:
|
24 |
+
embedding = OpenAIEmbeddings()
|
25 |
+
vstore = AstraDBVectorStore(
|
26 |
+
embedding=embedding,
|
27 |
+
collection_name=os.environ.get("ASTRA_DB_COLLECTION"),
|
28 |
+
token=os.environ.get("ASTRA_DB_APPLICATION_TOKEN"),
|
29 |
+
api_endpoint=os.environ.get("ASTRA_DB_API_ENDPOINT"),
|
30 |
+
)
|
31 |
+
|
32 |
+
retriever = vstore.as_retriever(search_kwargs={'k': 10})
|
33 |
+
else:
|
34 |
+
retriever = RunnableLambda(just_read)
|
35 |
+
|
36 |
+
prompt_chain = (
|
37 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
38 |
+
| RunnableLambda(format_context)
|
39 |
+
| prompt
|
40 |
+
# | llm
|
41 |
+
# | StrOutputParser()
|
42 |
+
)
|
43 |
+
|
44 |
+
def group_and_sort(documents):
|
45 |
+
grouped = {}
|
46 |
+
for document in documents:
|
47 |
+
title = document.metadata["Title"]
|
48 |
+
docs = grouped.get(title, [])
|
49 |
+
grouped[title] = docs
|
50 |
+
|
51 |
+
docs.append((document.page_content, document.metadata["range"]))
|
52 |
+
|
53 |
+
for title, values in grouped.items():
|
54 |
+
values.sort(key=lambda doc:doc[1][0])
|
55 |
+
|
56 |
+
for title in grouped:
|
57 |
+
text = ''
|
58 |
+
prev_last = 0
|
59 |
+
for fragment, (start, last) in grouped[title]:
|
60 |
+
if start < prev_last:
|
61 |
+
text += fragment[prev_last-start:]
|
62 |
+
elif start == prev_last:
|
63 |
+
text += fragment
|
64 |
+
else:
|
65 |
+
text += ' [...] '
|
66 |
+
text += fragment
|
67 |
+
prev_last = last
|
68 |
+
|
69 |
+
grouped[title] = text
|
70 |
+
|
71 |
+
return grouped
|
72 |
+
|
73 |
+
def format_context(pipeline_state):
|
74 |
+
"""Print the state passed between Runnables in a langchain and pass it on"""
|
75 |
+
|
76 |
+
context = ''
|
77 |
+
documents = group_and_sort(pipeline_state["context"])
|
78 |
+
for title, text in documents.items():
|
79 |
+
context += f"\nTitle: {title}\n"
|
80 |
+
context += text
|
81 |
+
context += '\n\n---\n'
|
82 |
+
|
83 |
+
pipeline_state["context"] = context
|
84 |
+
return pipeline_state
|
85 |
+
|
86 |
+
def just_read(pipeline_state):
|
87 |
+
fname = "docs.pickle"
|
88 |
+
import pickle
|
89 |
+
|
90 |
+
return pickle.load(open(fname, "rb"))
|
91 |
+
|
92 |
+
def new_state():
|
93 |
+
return gr.State({
|
94 |
+
"system": None,
|
95 |
+
})
|
96 |
+
|
97 |
+
def chat(message, history, state):
|
98 |
+
if not history:
|
99 |
+
system_prompt = prompt_chain.invoke(message)
|
100 |
+
system_prompt = system_prompt.messages[0]
|
101 |
+
state["system"] = system_prompt
|
102 |
+
else:
|
103 |
+
system_prompt = state["system"]
|
104 |
+
|
105 |
+
messages = [system_prompt]
|
106 |
+
for human, ai in history:
|
107 |
+
messages.append(HumanMessage(human))
|
108 |
+
messages.append(AIMessage(ai))
|
109 |
+
messages.append(HumanMessage(message))
|
110 |
+
|
111 |
+
all = ''
|
112 |
+
for response in llm.stream(messages):
|
113 |
+
all += response.content
|
114 |
+
yield all
|
115 |
+
|
116 |
+
def gr_main():
|
117 |
+
theme = gr.Theme.from_hub("freddyaboulton/[email protected]")
|
118 |
+
theme.set(
|
119 |
+
color_accent_soft="#818eb6", # ChatBot.svelte / .message-row.panel.user-row
|
120 |
+
background_fill_secondary="#6272a4", # ChatBot.svelte / .message-row.panel.bot-row
|
121 |
+
button_primary_text_color="*button_secondary_text_color",
|
122 |
+
button_primary_background_fill="*button_secondary_background_fill")
|
123 |
+
|
124 |
+
with gr.Blocks(
|
125 |
+
title="Sherlock Holmes stories",
|
126 |
+
fill_height=True,
|
127 |
+
theme=theme
|
128 |
+
) as app:
|
129 |
+
state = new_state()
|
130 |
+
gr.ChatInterface(
|
131 |
+
chat,
|
132 |
+
chatbot=gr.Chatbot(show_label=False, render=False, scale=1),
|
133 |
+
title="Sherlock Holmes stories",
|
134 |
+
examples=[
|
135 |
+
["I arrived late last night and found a dead goose in my bed"],
|
136 |
+
["Help please sir. I'm about to get married, to the most lovely lady,"
|
137 |
+
"and I just received a letter threatening me to make public some things"
|
138 |
+
"of my past I'd rather keep quiet, unless I don't marry"],
|
139 |
+
],
|
140 |
+
additional_inputs=[state])
|
141 |
+
app.launch(show_api=False)
|
142 |
+
if __name__ == "__main__":
|
143 |
+
ai_setup()
|
144 |
+
gr_main()
|
requirements.txt
CHANGED
@@ -1 +1 @@
|
|
1 |
-
|
|
|
1 |
+
ragstack-ai
|