Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
-
import os
|
2 |
-
from dotenv import load_dotenv
|
3 |
-
load_dotenv()
|
4 |
-
|
5 |
from langchain_core.tools import tool
|
6 |
from langgraph.graph import StateGraph, START, MessagesState
|
7 |
from langgraph.prebuilt import tools_condition, ToolNode
|
8 |
from langchain_groq import ChatGroq
|
9 |
from langchain_core.messages import HumanMessage, SystemMessage
|
10 |
import math
|
|
|
|
|
|
|
|
|
11 |
|
12 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
13 |
serpapi_api_key = os.getenv("SERPAPI_API_KEY")
|
@@ -17,28 +17,34 @@ serpapi_api_key = os.getenv("SERPAPI_API_KEY")
|
|
17 |
# -------------------------
|
18 |
@tool
|
19 |
def add(a: float, b: float) -> float:
|
|
|
20 |
return a + b
|
21 |
|
22 |
@tool
|
23 |
def subtract(a: float, b: float) -> float:
|
|
|
24 |
return a - b
|
25 |
|
26 |
@tool
|
27 |
def multiply(a: float, b: float) -> float:
|
|
|
28 |
return a * b
|
29 |
|
30 |
@tool
|
31 |
def divide(a: float, b: float) -> float:
|
|
|
32 |
if b == 0:
|
33 |
return float('inf')
|
34 |
return a / b
|
35 |
|
36 |
@tool
|
37 |
def modulus(a: int, b: int) -> int:
|
|
|
38 |
return a % b
|
39 |
|
40 |
@tool
|
41 |
def python_eval(code: str) -> str:
|
|
|
42 |
try:
|
43 |
result = eval(code)
|
44 |
return f"Result: {result}"
|
@@ -47,18 +53,22 @@ def python_eval(code: str) -> str:
|
|
47 |
|
48 |
@tool
|
49 |
def translate_to_arabic(text: str) -> str:
|
|
|
50 |
return f"Arabic translation of '{text}'"
|
51 |
|
52 |
@tool
|
53 |
def translate_to_english(text: str) -> str:
|
|
|
54 |
return f"English translation of '{text}'"
|
55 |
|
56 |
@tool
|
57 |
def summarize_text(text: str) -> str:
|
|
|
58 |
return f"Summary: {text[:100]}..."
|
59 |
|
60 |
@tool
|
61 |
def analyze_sentiment(text: str) -> str:
|
|
|
62 |
if any(word in text.lower() for word in ["good", "great", "excellent", "happy"]):
|
63 |
return "Sentiment: Positive"
|
64 |
elif any(word in text.lower() for word in ["bad", "terrible", "sad", "hate"]):
|
@@ -67,6 +77,7 @@ def analyze_sentiment(text: str) -> str:
|
|
67 |
|
68 |
@tool
|
69 |
def speech_to_text_stub(audio: str) -> str:
|
|
|
70 |
return "Converted audio to text: (This is a placeholder result)"
|
71 |
|
72 |
# -------------------------
|
@@ -110,5 +121,7 @@ def build_deepseek_graph():
|
|
110 |
builder.add_conditional_edges("assistant", tools_condition)
|
111 |
builder.add_edge("tools", "assistant")
|
112 |
|
113 |
-
ninu = builder.compile()
|
114 |
return ninu
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from langchain_core.tools import tool
|
2 |
from langgraph.graph import StateGraph, START, MessagesState
|
3 |
from langgraph.prebuilt import tools_condition, ToolNode
|
4 |
from langchain_groq import ChatGroq
|
5 |
from langchain_core.messages import HumanMessage, SystemMessage
|
6 |
import math
|
7 |
+
import os
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
load_dotenv()
|
11 |
|
12 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
13 |
serpapi_api_key = os.getenv("SERPAPI_API_KEY")
|
|
|
17 |
# -------------------------
|
18 |
@tool
|
19 |
def add(a: float, b: float) -> float:
|
20 |
+
"""Adds two numbers and returns the sum."""
|
21 |
return a + b
|
22 |
|
23 |
@tool
|
24 |
def subtract(a: float, b: float) -> float:
|
25 |
+
"""Subtracts second number from first and returns the difference."""
|
26 |
return a - b
|
27 |
|
28 |
@tool
|
29 |
def multiply(a: float, b: float) -> float:
|
30 |
+
"""Multiplies two numbers and returns the product."""
|
31 |
return a * b
|
32 |
|
33 |
@tool
|
34 |
def divide(a: float, b: float) -> float:
|
35 |
+
"""Divides first number by second and returns the quotient. Returns infinity if divisor is zero."""
|
36 |
if b == 0:
|
37 |
return float('inf')
|
38 |
return a / b
|
39 |
|
40 |
@tool
|
41 |
def modulus(a: int, b: int) -> int:
|
42 |
+
"""Returns the modulus (remainder) of a divided by b."""
|
43 |
return a % b
|
44 |
|
45 |
@tool
|
46 |
def python_eval(code: str) -> str:
|
47 |
+
"""Evaluates a Python expression and returns the result or error message."""
|
48 |
try:
|
49 |
result = eval(code)
|
50 |
return f"Result: {result}"
|
|
|
53 |
|
54 |
@tool
|
55 |
def translate_to_arabic(text: str) -> str:
|
56 |
+
"""Returns a placeholder Arabic translation of the input text."""
|
57 |
return f"Arabic translation of '{text}'"
|
58 |
|
59 |
@tool
|
60 |
def translate_to_english(text: str) -> str:
|
61 |
+
"""Returns a placeholder English translation of the input text."""
|
62 |
return f"English translation of '{text}'"
|
63 |
|
64 |
@tool
|
65 |
def summarize_text(text: str) -> str:
|
66 |
+
"""Returns a summary (first 100 characters) of the input text."""
|
67 |
return f"Summary: {text[:100]}..."
|
68 |
|
69 |
@tool
|
70 |
def analyze_sentiment(text: str) -> str:
|
71 |
+
"""Analyzes the sentiment of the text and returns Positive, Negative, or Neutral."""
|
72 |
if any(word in text.lower() for word in ["good", "great", "excellent", "happy"]):
|
73 |
return "Sentiment: Positive"
|
74 |
elif any(word in text.lower() for word in ["bad", "terrible", "sad", "hate"]):
|
|
|
77 |
|
78 |
@tool
|
79 |
def speech_to_text_stub(audio: str) -> str:
|
80 |
+
"""A placeholder tool to convert audio input to text."""
|
81 |
return "Converted audio to text: (This is a placeholder result)"
|
82 |
|
83 |
# -------------------------
|
|
|
121 |
builder.add_conditional_edges("assistant", tools_condition)
|
122 |
builder.add_edge("tools", "assistant")
|
123 |
|
124 |
+
ninu = builder.compile()
|
125 |
return ninu
|
126 |
+
|
127 |
+
|