Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
"""LangGraph Agent with OpenAI"""
|
| 2 |
import os
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
from langgraph.graph import START, StateGraph, MessagesState
|
| 5 |
from langgraph.prebuilt import tools_condition, ToolNode
|
| 6 |
from langchain_openai import ChatOpenAI
|
|
@@ -9,8 +8,6 @@ from langchain_community.document_loaders import ArxivLoader
|
|
| 9 |
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
|
| 10 |
from langchain_core.tools import tool
|
| 11 |
|
| 12 |
-
load_dotenv()
|
| 13 |
-
|
| 14 |
# Tools definition
|
| 15 |
@tool
|
| 16 |
def multiply(a: int, b: int) -> int:
|
|
@@ -73,9 +70,12 @@ def wiki_search(query: str) -> str:
|
|
| 73 |
"""
|
| 74 |
try:
|
| 75 |
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
|
|
|
|
|
|
|
|
|
| 76 |
formatted_search_docs = "\n\n---\n\n".join(
|
| 77 |
[
|
| 78 |
-
f'
|
| 79 |
for doc in search_docs
|
| 80 |
])
|
| 81 |
return formatted_search_docs
|
|
@@ -93,9 +93,12 @@ def arxiv_search(query: str) -> str:
|
|
| 93 |
"""
|
| 94 |
try:
|
| 95 |
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
|
|
|
|
|
|
|
|
|
| 96 |
formatted_search_docs = "\n\n---\n\n".join(
|
| 97 |
[
|
| 98 |
-
f'
|
| 99 |
for doc in search_docs
|
| 100 |
])
|
| 101 |
return formatted_search_docs
|
|
@@ -103,7 +106,11 @@ def arxiv_search(query: str) -> str:
|
|
| 103 |
return f"Error searching Arxiv: {str(e)}"
|
| 104 |
|
| 105 |
# System prompt
|
| 106 |
-
system_prompt = """You are a
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# Tools list
|
| 109 |
tools = [
|
|
@@ -136,8 +143,8 @@ class LangGraphAgent:
|
|
| 136 |
"""Build the LangGraph workflow"""
|
| 137 |
# Initialize OpenAI LLM
|
| 138 |
llm = ChatOpenAI(
|
| 139 |
-
model="gpt-4
|
| 140 |
-
temperature=0
|
| 141 |
api_key=self.api_key
|
| 142 |
)
|
| 143 |
|
|
@@ -215,6 +222,11 @@ BasicAgent = LangGraphAgent
|
|
| 215 |
if __name__ == "__main__":
|
| 216 |
# Test the agent
|
| 217 |
print("Testing LangGraphAgent...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
try:
|
| 219 |
agent = LangGraphAgent()
|
| 220 |
test_questions = [
|
|
|
|
| 1 |
"""LangGraph Agent with OpenAI"""
|
| 2 |
import os
|
|
|
|
| 3 |
from langgraph.graph import START, StateGraph, MessagesState
|
| 4 |
from langgraph.prebuilt import tools_condition, ToolNode
|
| 5 |
from langchain_openai import ChatOpenAI
|
|
|
|
| 8 |
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
|
| 9 |
from langchain_core.tools import tool
|
| 10 |
|
|
|
|
|
|
|
| 11 |
# Tools definition
|
| 12 |
@tool
|
| 13 |
def multiply(a: int, b: int) -> int:
|
|
|
|
| 70 |
"""
|
| 71 |
try:
|
| 72 |
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 73 |
+
if not search_docs:
|
| 74 |
+
return f"No Wikipedia results found for: {query}"
|
| 75 |
+
|
| 76 |
formatted_search_docs = "\n\n---\n\n".join(
|
| 77 |
[
|
| 78 |
+
f'Source: {doc.metadata.get("source", "Wikipedia")}\nContent: {doc.page_content[:2000]}...'
|
| 79 |
for doc in search_docs
|
| 80 |
])
|
| 81 |
return formatted_search_docs
|
|
|
|
| 93 |
"""
|
| 94 |
try:
|
| 95 |
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 96 |
+
if not search_docs:
|
| 97 |
+
return f"No Arxiv results found for: {query}"
|
| 98 |
+
|
| 99 |
formatted_search_docs = "\n\n---\n\n".join(
|
| 100 |
[
|
| 101 |
+
f'Title: {doc.metadata.get("Title", "Unknown")}\nAuthors: {doc.metadata.get("Authors", "Unknown")}\nContent: {doc.page_content[:1500]}...'
|
| 102 |
for doc in search_docs
|
| 103 |
])
|
| 104 |
return formatted_search_docs
|
|
|
|
| 106 |
return f"Error searching Arxiv: {str(e)}"
|
| 107 |
|
| 108 |
# System prompt
|
| 109 |
+
system_prompt = """You are a helpful AI assistant with access to various tools for calculations and information retrieval.
|
| 110 |
+
You can perform mathematical operations, search Wikipedia, and search academic papers on Arxiv.
|
| 111 |
+
Always try to provide accurate, concise, and helpful responses based on the tools available to you.
|
| 112 |
+
When searching for information, be thorough but concise in your final answer.
|
| 113 |
+
If a question requires multiple steps or tools, break it down and use the appropriate tools in sequence."""
|
| 114 |
|
| 115 |
# Tools list
|
| 116 |
tools = [
|
|
|
|
| 143 |
"""Build the LangGraph workflow"""
|
| 144 |
# Initialize OpenAI LLM
|
| 145 |
llm = ChatOpenAI(
|
| 146 |
+
model="gpt-4-turbo", # Changed from gpt-4-turbo-preview
|
| 147 |
+
temperature=0,
|
| 148 |
api_key=self.api_key
|
| 149 |
)
|
| 150 |
|
|
|
|
| 222 |
if __name__ == "__main__":
|
| 223 |
# Test the agent
|
| 224 |
print("Testing LangGraphAgent...")
|
| 225 |
+
if not os.environ.get("OPENAI_KEY"):
|
| 226 |
+
print("Error: OPENAI_KEY environment variable not set")
|
| 227 |
+
print("Please set it with: export OPENAI_KEY=your-openai-api-key")
|
| 228 |
+
exit(1)
|
| 229 |
+
|
| 230 |
try:
|
| 231 |
agent = LangGraphAgent()
|
| 232 |
test_questions = [
|