Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from langchain_community.tools import WikipediaQueryRun
|
4 |
+
from langchain_community.utilities import WikipediaAPIWrapper
|
5 |
+
from langchain_groq import ChatGroq
|
6 |
+
from langchain import hub
|
7 |
+
from langchain.agents import create_tool_calling_agent, AgentExecutor
|
8 |
+
|
9 |
+
api_wrapper = WikipediaAPIWrapper(top_k_results=2)
|
10 |
+
wiki_tool = WikipediaQueryRun(api_wrapper=api_wrapper)
|
11 |
+
|
12 |
+
# Wikipedia Search Tool
|
13 |
+
tools = [wiki_tool]
|
14 |
+
|
15 |
+
GROQ_API_KEY = os.environ["GROQ_API_KEY"]
|
16 |
+
llm = ChatGroq(
|
17 |
+
model="mixtral-8x7b-32768",
|
18 |
+
temperature=0,
|
19 |
+
max_tokens=None,
|
20 |
+
timeout=None,
|
21 |
+
max_retries=2,
|
22 |
+
api_key=GROQ_API_KEY
|
23 |
+
)
|
24 |
+
|
25 |
+
prompt = hub.pull("hwchase17/openai-tools-agent")
|
26 |
+
prompt.pretty_print()
|
27 |
+
|
28 |
+
agent = create_tool_calling_agent(llm=llm, tools=tools, prompt=prompt)
|
29 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False)
|
30 |
+
|
31 |
+
def generate(query, api_key=""):
|
32 |
+
output = agent_executor.invoke({"input": query})["output"]
|
33 |
+
return output
|
34 |
+
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown("""
|
37 |
+
## Question Answering Agent with GROQ, Mixtral-8x7B, and LangChain
|
38 |
+
|
39 |
+
This is general question answering agent was created using Mixtral-8x7B LLM through GROQ, a Wikipedia search tool, and LangChain.
|
40 |
+
""")
|
41 |
+
gr.Markdown("#### Enter your question")
|
42 |
+
with gr.Row():
|
43 |
+
with gr.Column():
|
44 |
+
ques = gr.Textbox(label="Question", placeholder="Enter text here", lines=2)
|
45 |
+
with gr.Column():
|
46 |
+
ans = gr.Textbox(label="Answer", lines=4, interactive=False)
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column():
|
49 |
+
btn = gr.Button("Submit")
|
50 |
+
with gr.Column():
|
51 |
+
clear = gr.ClearButton([ques, ans])
|
52 |
+
|
53 |
+
btn.click(fn=generate, inputs=[ques], outputs=[ans])
|
54 |
+
examples = gr.Examples(
|
55 |
+
examples=[
|
56 |
+
"When is Leonhard Euler's birthday?",
|
57 |
+
"Who were the 3 main characters in GTA V?",
|
58 |
+
"Who was the voice actor for Kratos in God of War: Ragnarok?",
|
59 |
+
"How much did 'Deadpool and Wolverine' make at the global box office?",
|
60 |
+
"Who was the last monarch of Ethiopia?",
|
61 |
+
],
|
62 |
+
inputs=[ques],
|
63 |
+
)
|
64 |
+
|
65 |
+
demo.queue().launch(debug=True)
|