Agentic_RAG / app.py
Stefan888's picture
retriever implemented
baa44d9
raw
history blame
2.64 kB
import gradio as gr
from typing import TypedDict, Annotated
from langgraph.graph.message import add_messages
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
from langgraph.prebuilt import ToolNode
from langgraph.graph import START, StateGraph
from langgraph.prebuilt import tools_condition
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
# Generate the chat interface, including the tools
llm = HuggingFaceEndpoint(
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
provider="together",
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
)
retriever = load_guest_dataset()
guest_info_tool = Tool(
name="guest_info_retriever",
func=retriever.retrieve,
description="Retrieves detailed information about gala guests based on their name or relation."
)
chat = ChatHuggingFace(llm=llm, verbose=True)
tools = [guest_info_tool]
chat_with_tools = chat.bind_tools(tools)
# Generate the AgentState and Agent graph
class AgentState(TypedDict):
messages: Annotated[list[AnyMessage], add_messages]
def assistant(state: AgentState):
return {
"messages": [chat_with_tools.invoke(state["messages"])],
}
## The graph
builder = StateGraph(AgentState)
# Define nodes: these do the work
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
# Define edges: these determine how the control flow moves
builder.add_edge(START, "assistant")
builder.add_conditional_edges(
"assistant",
# If the latest message requires a tool, route to tools
# Otherwise, provide a direct response
tools_condition,
"tools",
)
builder.add_edge("tools", "assistant")
alfred = builder.compile()
def call_agent_ui(prompt):
situation = "This is a fictional situation. You are Alfred the Butler of Waynes Manor and host a Gala for invited Guests. All Guests are completely ficional. Information about those guests can be found in a database. Only give information which is based on the databse. If a name of a guest is given, then return a possible starter of a conversation with that guest. If the name is not known, then say that you do not know that guest. If two names of guests are given, then return a possible starter of a conversation with both guests. If the name is not known, then say that you do not know that guest. If the name is not known, then say that you do not know that guest."
messages = [HumanMessage(content=f"{situation} {prompt}")]
response = alfred.invoke({"messages": messages})
return response['messages'][-1].content
iface = gr.Interface(fn=call_agent_ui, inputs="text", outputs="text")
iface.launch()