File size: 2,173 Bytes
b27e41a 41dc76f b27e41a 02e5fa7 b27e41a 41dc76f b27e41a 682e444 b27e41a fd00cce b27e41a |
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import gradio as gr
import os
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from langchain_groq import ChatGroq
from langchain import hub
from langchain.agents import create_tool_calling_agent, AgentExecutor
api_wrapper = WikipediaAPIWrapper(top_k_results=1)
wiki_tool = WikipediaQueryRun(api_wrapper=api_wrapper)
# Wikipedia Search Tool
tools = [wiki_tool]
GROQ_API_KEY = os.environ["GROQ_API_KEY"]
llm = ChatGroq(
model="mixtral-8x7b-32768",
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
api_key=GROQ_API_KEY
)
prompt = hub.pull("hwchase17/openai-tools-agent")
prompt.pretty_print()
agent = create_tool_calling_agent(llm=llm, tools=tools, prompt=prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False)
def generate(query):
if query.strip() == "":
return "Enter your question"
output = agent_executor.invoke({"input": query})["output"]
return output
with gr.Blocks() as demo:
gr.Markdown("""
## Wikipedia Agent with GROQ, Mixtral-8x7B, and LangChain
This general question answering agent was created using Mixtral-8x7B LLM through GROQ, a Wikipedia search tool, and LangChain.
""")
gr.Markdown("#### Enter your question")
with gr.Row():
with gr.Column():
ques = gr.Textbox(label="Question", placeholder="Enter text here", lines=2)
with gr.Column():
ans = gr.Textbox(label="Answer", lines=4, interactive=False)
with gr.Row():
with gr.Column():
btn = gr.Button("Submit")
with gr.Column():
clear = gr.ClearButton([ques, ans])
btn.click(fn=generate, inputs=[ques], outputs=[ans])
examples = gr.Examples(
examples=[
"Where did Jim Simons get his PhD?",
"When is Leonhard Euler's birthday?",
"Who were the 3 main characters in GTA V?",
"Who was the voice actor for Kratos in God of War: Ragnarok?",
"How much did 'Deadpool and Wolverine' make at the global box office?",
"Who was the last monarch of Ethiopia?",
],
inputs=[ques],
)
demo.queue().launch(debug=True) |