File size: 3,811 Bytes
9a62fc6
38812af
0ef1947
 
 
 
 
 
 
 
 
 
f0ca6d7
0ef1947
 
 
 
 
38812af
 
0ef1947
38812af
 
0ef1947
 
 
 
 
 
 
 
 
 
 
 
38812af
f0ca6d7
 
0ef1947
 
f0ca6d7
 
0ef1947
 
38812af
 
 
 
 
 
0ef1947
 
 
 
 
 
38812af
0ef1947
 
 
 
38812af
9a62fc6
0ef1947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501c4dc
494cab7
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import gradio as gr
import random
from dotenv import load_dotenv
import os

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
from langchain_community.embeddings import HuggingFaceEmbeddings

from langgraph.prebuilt import create_react_agent
from langgraph.store.memory import InMemoryStore



# Import our custom tools from their modules
from tools import weather_info_tool, hub_stats_tool, duckduckgo_search_tool
from retriever import load_guest_dataset

# Load environment variables from .env file
load_dotenv()

HUGGINGFACEHUB_API_TOKEN=os.getenv("HUGGINGFACE_TOKEN")

# Generate the chat interface, including the tools
llm = HuggingFaceEndpoint(
    repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
    huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
)

chat = ChatHuggingFace(llm=llm, verbose=True)

# Initialize memory store with HuggingFace embeddings
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
store = InMemoryStore(
    index={
        "dims": 384,  # Dimension for the MiniLM model
        "embed": embeddings,
    }
) 



# Load the guest dataset and initialize the guest info tool
guest_info_tool = load_guest_dataset()




tools = [guest_info_tool, weather_info_tool, hub_stats_tool, duckduckgo_search_tool]
chat_with_tools = chat.bind_tools(tools)

# Create Alfred with all the tools
agent = create_react_agent(
    "openai:gpt-4o-mini",
    tools=tools,
    store=store,
)

# Generate the AgentState and Agent graph
class AgentState(TypedDict):
    messages: Annotated[list[AnyMessage], add_messages]

def assistant(state: AgentState):
    # Prepare messages for the agent

    messages = [{"role": "user", "content": msg.content} for msg in state["messages"]]
    
    # Invoke the agent with the prepared messages
    response = agent.invoke({"messages": messages})
    print(response)
    
    # Ensure the response is a list of message dictionaries
    response_messages = [
        {"role": "assistant", "content": msg.content} for msg in response["messages"]
    ]
    print(response_messages)

    # Extract the response content from the last message
    response_content = response_messages[-1]["content"]
    print(response_content)
    
    return {
        "messages": [response_content],
    }


## The graph
builder = StateGraph(AgentState)

# Define nodes: these do the work
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))

# Define the graph

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,
)

builder.add_edge("tools", "assistant")
alfred = builder.compile()



def GradioUI(chain):
    with gr.Blocks() as demo:
        chatbot = gr.Chatbot()
        msg = gr.Textbox()
        clear = gr.Button("Clear")

        def user(user_message, history):
            return "", history + [[user_message, None]]

        def bot(history):
            messages = [HumanMessage(content=history[-1][0])]
            response = chain.invoke({"messages": messages})
            bot_message = response["messages"][-1].content
            history[-1][1] = bot_message
            return history

        msg.submit(user, [msg, chatbot], [msg, chatbot]).then(
            bot, chatbot, chatbot
        )
        clear.click(lambda: None, None, chatbot, queue=False)

    return demo


if __name__ == "__main__":
    GradioUI(alfred).launch()