File size: 2,915 Bytes
d02200f
 
 
 
 
 
 
 
 
b6c03a5
 
 
 
 
 
 
d02200f
 
c11970b
d02200f
 
 
 
 
b6c03a5
d02200f
b6c03a5
 
 
d02200f
 
 
 
 
b6c03a5
 
 
 
 
 
 
 
 
 
 
 
 
d02200f
 
b6c03a5
d02200f
 
 
b6c03a5
 
d02200f
 
 
 
 
 
 
 
b6c03a5
d02200f
 
 
b6c03a5
 
 
 
 
 
 
 
 
d02200f
 
 
 
 
 
 
b6c03a5
 
 
 
d02200f
 
 
 
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
from dotenv import load_dotenv

load_dotenv()


from langgraph.graph import START, StateGraph, MessagesState
from langgraph.prebuilt import tools_condition
from langgraph.prebuilt import ToolNode
from langchain_core.messages import SystemMessage, HumanMessage
from tools.searchtools import wiki_search, web_search, arxiv_search, vector_store
from tools.mathtools import multiply, add, subtract, divide, modulus,power,square_root
from tools.codetools import execute_code_multilang
from tools.documenttools import save_and_read_file,download_file_from_url, extract_text_from_image, analyze_csv_file, analyze_excel_file
from tools.imagetools import analyze_image, transform_image, draw_on_image, generate_simple_image, combine_images
from langchain_groq import ChatGroq
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint

# load the system prompt from the file
with open("system_prompt.txt", "r", encoding="utf-8") as f:
    system_prompt = f.read()

# System message
sys_msg = SystemMessage(content=system_prompt)


tools = [
    web_search,
    wiki_search,
    arxiv_search,
    multiply,
    add,
    subtract,
    divide,
    modulus,
    power,
    square_root,
    save_and_read_file,
    download_file_from_url,
    extract_text_from_image,
    analyze_csv_file,
    analyze_excel_file,
    execute_code_multilang,
    analyze_image,
    transform_image,
    draw_on_image,
    generate_simple_image,
    combine_images,
]


# Build graph function
def build_graph():
    """Build the graph"""
    # Load environment variables from .env file
    llm = ChatGroq(model="qwen-qwq-32b", temperature=0)

    # Bind tools to LLM
    llm_with_tools = llm.bind_tools(tools)

    # Node
    def assistant(state: MessagesState):
        """Assistant node"""
        return {"messages": [llm_with_tools.invoke(state["messages"])]}

    def retriever(state: MessagesState):
        """Retriever node"""
        similar_question = vector_store.similarity_search(state["messages"][0].content)

        if similar_question:  # Check if the list is not empty
            example_msg = HumanMessage(
                content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}",
            )
            return {"messages": [sys_msg] + state["messages"] + [example_msg]}
        else:
            # Handle the case when no similar questions are found
            return {"messages": [sys_msg] + state["messages"]}

    builder = StateGraph(MessagesState)
    builder.add_node("retriever", retriever)
    builder.add_node("assistant", assistant)
    builder.add_node("tools", ToolNode(tools))
    builder.add_edge(START, "retriever")
    builder.add_edge("retriever", "assistant")
    builder.add_conditional_edges(
        "assistant",
        tools_condition,
    )
    builder.add_edge("tools", "assistant")

    # Compile graph
    return builder.compile()