File size: 3,017 Bytes
f158561 9d05c23 a025ba2 66fdcfd f158561 9d05c23 f158561 9d05c23 f158561 a025ba2 f158561 4c4f363 f158561 9d05c23 f158561 3b64660 a025ba2 f158561 a025ba2 f158561 3b64660 9d05c23 aa8acb6 9d05c23 3b64660 f158561 c528499 9d05c23 a025ba2 9d05c23 dc2aa07 9d05c23 f158561 9d05c23 f158561 3b64660 f158561 4c4f363 3b64660 9d05c23 a025ba2 |
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
import gradio as gr
import os
import wikipedia
from langchain_community.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.agents import AgentExecutor, initialize_agent
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.tools import Tool
# Define tools
def create_your_own(query: str) -> str:
"""This function can do whatever you would like once you fill it in"""
return query[::-1]
def get_current_temperature(query: str) -> str:
return "It's sunny and 75°F."
def search_wikipedia(query: str) -> str:
try:
summary = wikipedia.summary(query, sentences=2)
return summary
except wikipedia.exceptions.DisambiguationError as e:
return f"Multiple results found: {', '.join(e.options[:5])}"
except wikipedia.exceptions.PageError:
return "No relevant Wikipedia page found."
tools = [
Tool(name="Temperature", func=get_current_temperature, description="Get current temperature"),
Tool(name="Search Wikipedia", func=search_wikipedia, description="Search Wikipedia"),
Tool(name="Create Your Own", func=create_your_own, description="Custom tool for processing input")
]
# Define chatbot class
class cbfs:
def __init__(self, tools):
self.model = ChatOpenAI(temperature=0, openai_api_key=os.getenv("OPENAI_API_KEY"))
self.memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history", ai_prefix="Assistant")
self.prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful but sassy assistant. Remember what the user tells you in the conversation."),
MessagesPlaceholder(variable_name="chat_history"),
("user", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad")
])
self.chain = initialize_agent(
tools=tools,
llm=self.model,
agent="zero-shot-react-description",
verbose=True,
memory=self.memory
)
def convchain(self, query):
if not query:
return "Please enter a query."
try:
result = self.chain.invoke({"input": query})
response = result.get("output", "No response generated.")
self.memory.save_context({"input": query}, {"output": response})
print("Agent Execution Result:", response) # Debugging output
return response
except Exception as e:
print("Execution Error:", str(e))
return f"Error: {str(e)}"
# Create chatbot instance
cb = cbfs(tools)
def process_query(query):
return cb.convchain(query)
# Define Gradio UI
with gr.Blocks() as demo:
with gr.Row():
inp = gr.Textbox(placeholder="Enter text here…", label="User Input")
output = gr.Textbox(placeholder="Response...", label="ChatBot Output", interactive=False)
inp.submit(process_query, inputs=inp, outputs=output)
demo.launch(share=True)
|