JetzeJ commited on
Commit
115aedf
·
1 Parent(s): f5af257

version 1

Browse files
Files changed (5) hide show
  1. .gitignore +4 -0
  2. app.py +57 -5
  3. requirements.txt +9 -0
  4. retriever.py +18 -0
  5. tools.py +19 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ .env
2
+ __pycache__/
3
+ *.pyc
4
+ .DS_Store
app.py CHANGED
@@ -1,7 +1,59 @@
1
- import gradio as gr
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import TypedDict, Annotated
2
+ from langgraph.graph.message import add_messages
3
+ from langchain_core.messages import AnyMessage, HumanMessage, AIMessage
4
+ from langgraph.prebuilt import ToolNode
5
+ from langgraph.graph import START, StateGraph
6
+ from langgraph.prebuilt import tools_condition
7
+ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
8
 
9
+ # Generate the chat interface, including the tools
10
+ llm = HuggingFaceEndpoint(
11
+ repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
12
+ huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
13
+ )
14
 
15
+ chat = ChatHuggingFace(llm=llm, verbose=True)
16
+ tools = [guest_info_tool]
17
+ chat_with_tools = chat.bind_tools(tools)
18
+
19
+ # Generate the AgentState and Agent graph
20
+ class AgentState(TypedDict):
21
+ messages: Annotated[list[AnyMessage], add_messages]
22
+
23
+ def assistant(state: AgentState):
24
+ return {
25
+ "messages": [chat_with_tools.invoke(state["messages"])],
26
+ }
27
+
28
+ ## The graph
29
+ builder = StateGraph(AgentState)
30
+
31
+ # Define nodes: these do the work
32
+ builder.add_node("assistant", assistant)
33
+ builder.add_node("tools", ToolNode(tools))
34
+
35
+ # Define edges: these determine how the control flow moves
36
+ builder.add_edge(START, "assistant")
37
+ builder.add_conditional_edges(
38
+ "assistant",
39
+ # If the latest message requires a tool, route to tools
40
+ # Otherwise, provide a direct response
41
+ tools_condition,
42
+ )
43
+ builder.add_edge("tools", "assistant")
44
+ alfred = builder.compile()
45
+
46
+ def respond(message, history):
47
+ messages = [HumanMessage(content=message)]
48
+ response = alfred.invoke({"messages": messages})
49
+ return response["messages"][-1].content
50
+
51
+
52
+ demo = gr.ChatInterface(
53
+ fn=respond,
54
+ title="Alfred - Your Gala Assistant",
55
+ description="Ask Alfred about your gala guests. Try: 'Tell me about Lady Ada Lovelace.'",
56
+ )
57
+
58
+ if __name__ == "__main__":
59
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ langchain-community
3
+ langchain-core
4
+ langchain-huggingface
5
+ langgraph
6
+ datasets
7
+ rank-bm25
8
+ python-dotenv
9
+ huggingface-hub
retriever.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.retrievers import BM25Retriever
2
+ from langchain_core.tools import Tool
3
+
4
+ bm25_retriever = BM25Retriever.from_documents(docs)
5
+
6
+ def extract_text(query: str) -> str:
7
+ """Retrieves detailed information about gala guests based on their name or relation."""
8
+ results = bm25_retriever.invoke(query)
9
+ if results:
10
+ return "\n\n".join([doc.page_content for doc in results[:3]])
11
+ else:
12
+ return "No matching guest information found."
13
+
14
+ guest_info_tool = Tool(
15
+ name="guest_info_retriever",
16
+ func=extract_text,
17
+ description="Retrieves detailed information about gala guests based on their name or relation."
18
+ )
tools.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ from langchain_core.documents import Document
3
+
4
+ # Load the dataset
5
+ guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
6
+
7
+ # Convert dataset entries into Document objects
8
+ docs = [
9
+ Document(
10
+ page_content="\n".join([
11
+ f"Name: {guest['name']}",
12
+ f"Relation: {guest['relation']}",
13
+ f"Description: {guest['description']}",
14
+ f"Email: {guest['email']}"
15
+ ]),
16
+ metadata={"name": guest["name"]}
17
+ )
18
+ for guest in guest_dataset
19
+ ]