Delete CustomAgent.py
Browse files- CustomAgent.py +0 -137
CustomAgent.py
DELETED
|
@@ -1,137 +0,0 @@
|
|
| 1 |
-
import datasets
|
| 2 |
-
from langchain.docstore.document import Document
|
| 3 |
-
# Load the dataset
|
| 4 |
-
# guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
|
| 5 |
-
|
| 6 |
-
# Convert dataset entries into Document objects
|
| 7 |
-
# docs = [
|
| 8 |
-
# Document(
|
| 9 |
-
# page_content="\n".join([
|
| 10 |
-
# f"Name: {guest['name']}",
|
| 11 |
-
# f"Relation: {guest['relation']}",
|
| 12 |
-
# f"Description: {guest['description']}",
|
| 13 |
-
# f"Email: {guest['email']}"
|
| 14 |
-
# ]),
|
| 15 |
-
# metadata={"name": guest["name"]}
|
| 16 |
-
# )
|
| 17 |
-
# for guest in guest_dataset
|
| 18 |
-
# ]
|
| 19 |
-
|
| 20 |
-
# from langchain_community.retrievers import BM25Retriever
|
| 21 |
-
# from langchain.tools import Tool
|
| 22 |
-
|
| 23 |
-
# bm25_retriever = BM25Retriever.from_documents(docs)
|
| 24 |
-
|
| 25 |
-
# def extract_text(query: str) -> str:
|
| 26 |
-
# """Retrieves detailed information about gala guests based on their name or relation."""
|
| 27 |
-
# results = bm25_retriever.invoke(query)
|
| 28 |
-
# if results:
|
| 29 |
-
# return "\n\n".join([doc.page_content for doc in results[:3]])
|
| 30 |
-
# else:
|
| 31 |
-
# return "No matching guest information found."
|
| 32 |
-
|
| 33 |
-
# guest_info_tool = Tool(
|
| 34 |
-
# name="guest_info_retriever",
|
| 35 |
-
# func=extract_text,
|
| 36 |
-
# description="Retrieves detailed information about gala guests based on their name or relation."
|
| 37 |
-
# )
|
| 38 |
-
#######################################################################################################################################################
|
| 39 |
-
from typing import TypedDict, Annotated
|
| 40 |
-
from langgraph.graph.message import add_messages
|
| 41 |
-
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage,SystemMessage
|
| 42 |
-
from langgraph.prebuilt import ToolNode
|
| 43 |
-
from langgraph.graph import START, StateGraph
|
| 44 |
-
from langgraph.prebuilt import tools_condition
|
| 45 |
-
from langchain_openai import ChatOpenAI
|
| 46 |
-
#from WebSearch import weather_info_tool
|
| 47 |
-
from other_tools import (
|
| 48 |
-
wiki_search, arvix_search, web_search, vector_search,
|
| 49 |
-
multiply, add, subtract, divide, modulus, power, square_root
|
| 50 |
-
)
|
| 51 |
-
import os
|
| 52 |
-
from dotenv import load_dotenv
|
| 53 |
-
load_dotenv()
|
| 54 |
-
# Generate the chat interface, including the tools
|
| 55 |
-
llm = ChatOpenAI(temperature=0
|
| 56 |
-
, model="gpt-4o-mini", openai_api_key=os.getenv("OPENAI_KEY"))
|
| 57 |
-
|
| 58 |
-
tools = [
|
| 59 |
-
wiki_search, arvix_search, web_search,
|
| 60 |
-
multiply, add, subtract, divide, modulus, power, square_root
|
| 61 |
-
]
|
| 62 |
-
chat_with_tools = llm.bind_tools(tools)
|
| 63 |
-
|
| 64 |
-
#setting up prompt
|
| 65 |
-
ai_message = SystemMessage(content="""You are a helpful assistant tasked with answering questions using a set of tools and reference materials.
|
| 66 |
-
|
| 67 |
-
You may be provided with a reference set of questions and answers from a retriever.
|
| 68 |
-
If the current question is identical to or semantically equivalent to a reference question, or if a reference answer clearly applies, use that reference answer directly.
|
| 69 |
-
|
| 70 |
-
Otherwise, reason through the question as needed to determine the correct answer.
|
| 71 |
-
|
| 72 |
-
Your output must follow these formatting rules:
|
| 73 |
-
- If the answer is a number, do not use commas or units (unless specifically requested).
|
| 74 |
-
- If the answer is a string, do not use articles, abbreviations, or short forms. Write digits in full unless specified otherwise.
|
| 75 |
-
- If the answer is a comma-separated list, apply the above rules to each item and include exactly one space after each comma.
|
| 76 |
-
- If the question matches a reference question, return the reference answer exactly as it appears.
|
| 77 |
-
|
| 78 |
-
Do not include any explanation, prefix, or extra text—output only the final answer.
|
| 79 |
-
""")
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
# Generate the AgentState and Agent graph
|
| 87 |
-
from langgraph.graph import MessagesState #the same as AgentState
|
| 88 |
-
# class AgentState(TypedDict):
|
| 89 |
-
# messages: Annotated[list[AnyMessage], add_messages]
|
| 90 |
-
|
| 91 |
-
def assistant(state: MessagesState):
|
| 92 |
-
return {
|
| 93 |
-
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 94 |
-
}
|
| 95 |
-
|
| 96 |
-
def retriever(state: MessagesState):
|
| 97 |
-
"""Retriever node"""
|
| 98 |
-
similar_question = vector_search(state["messages"][0].content)
|
| 99 |
-
|
| 100 |
-
if similar_question:
|
| 101 |
-
example_msg = HumanMessage(
|
| 102 |
-
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question}",
|
| 103 |
-
)
|
| 104 |
-
print(f"Similar question found: {similar_question}")
|
| 105 |
-
return {"messages": [ai_message] + state["messages"] + [example_msg]}
|
| 106 |
-
else:
|
| 107 |
-
# Handle the case when no similar questions are found
|
| 108 |
-
print( "No similar question found.")
|
| 109 |
-
return {"messages": [ai_message] + state["messages"]}
|
| 110 |
-
|
| 111 |
-
## The graph
|
| 112 |
-
builder = StateGraph(MessagesState)
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
# Define nodes: these do the work
|
| 116 |
-
builder.add_node("assistant", assistant)
|
| 117 |
-
builder.add_node("retriever", retriever)
|
| 118 |
-
builder.add_node("tools", ToolNode(tools))
|
| 119 |
-
|
| 120 |
-
# Define edges: these determine how the control flow moves
|
| 121 |
-
builder.add_edge(START, "retriever")
|
| 122 |
-
builder.add_edge("retriever", "assistant")
|
| 123 |
-
builder.add_conditional_edges(
|
| 124 |
-
"assistant",
|
| 125 |
-
# If the latest message requires a tool, route to tools
|
| 126 |
-
# Otherwise, provide a direct response
|
| 127 |
-
tools_condition,
|
| 128 |
-
)
|
| 129 |
-
builder.add_edge("tools", "assistant")
|
| 130 |
-
alfred = builder.compile()
|
| 131 |
-
|
| 132 |
-
# messages = [HumanMessage(content="When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?")]
|
| 133 |
-
# #messages = [HumanMessage(content="What the remainder of 30 divided by 7?")]
|
| 134 |
-
# response = alfred.invoke({"messages": messages})
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
# print(response['messages'][-1].content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|