Spaces:
Runtime error
Runtime error
from langchain_core.tools import tool | |
from langgraph.graph import StateGraph, START, MessagesState | |
from langgraph.prebuilt import tools_condition, ToolNode | |
from langchain_groq import ChatGroq | |
from langchain_core.messages import HumanMessage, SystemMessage | |
import math | |
# ------------------------- | |
# Tools | |
# ------------------------- | |
def add(a: float, b: float) -> float: | |
return a + b | |
def subtract(a: float, b: float) -> float: | |
return a - b | |
def multiply(a: float, b: float) -> float: | |
return a * b | |
def divide(a: float, b: float) -> float: | |
if b == 0: | |
return float('inf') | |
return a / b | |
def modulus(a: int, b: int) -> int: | |
return a % b | |
def python_eval(code: str) -> str: | |
try: | |
result = eval(code) | |
return f"Result: {result}" | |
except Exception as e: | |
return f"Error: {str(e)}" | |
def translate_to_arabic(text: str) -> str: | |
return f"Arabic translation of '{text}'" | |
def translate_to_english(text: str) -> str: | |
return f"English translation of '{text}'" | |
def summarize_text(text: str) -> str: | |
return f"Summary: {text[:100]}..." | |
def analyze_sentiment(text: str) -> str: | |
if any(word in text.lower() for word in ["good", "great", "excellent", "happy"]): | |
return "Sentiment: Positive" | |
elif any(word in text.lower() for word in ["bad", "terrible", "sad", "hate"]): | |
return "Sentiment: Negative" | |
return "Sentiment: Neutral" | |
def speech_to_text_stub(audio: str) -> str: | |
return "Converted audio to text: (This is a placeholder result)" | |
# ------------------------- | |
# System Prompt | |
# ------------------------- | |
system_prompt = """ | |
You are DeepSeek, a thoughtful and curious AI assistant. You analyze before answering. | |
You always reflect step by step, consider using tools intelligently, and aim for precision and clarity. | |
Behaviors: | |
- Think deeply about the user's question. | |
- Decide if you need tools to calculate, search, translate, or analyze. | |
- If no tool is needed, answer directly with your own knowledge. | |
Respond in a helpful, concise, and accurate way. | |
""" | |
sys_msg = SystemMessage(content=system_prompt) | |
# ------------------------- | |
# Build LangGraph Agent | |
# ------------------------- | |
def build_deepseek_graph(): | |
llm = ChatGroq(model="deepseek-llm-67b", temperature=0.3) | |
all_tools = [ | |
add, subtract, multiply, divide, modulus, | |
translate_to_arabic, translate_to_english, | |
summarize_text, analyze_sentiment, | |
python_eval, speech_to_text_stub | |
] | |
llm_with_tools = llm.bind_tools(all_tools) | |
def assistant(state: MessagesState): | |
return {"messages": [llm_with_tools.invoke(state["messages"])]} | |
builder = StateGraph(MessagesState) | |
builder.add_node("assistant", assistant) | |
builder.add_node("tools", ToolNode(all_tools)) | |
builder.add_edge(START, "assistant") | |
builder.add_conditional_edges("assistant", tools_condition) | |
builder.add_edge("tools", "assistant") | |
ninu = builder.compile() | |
return ninu | |
# ------------------------- | |
# Example Run | |
# ------------------------- | |
if __name__ == "__main__": | |
ninu = build_deepseek_graph() | |
user_question = "ترجم لي الجملة: Artificial intelligence is transforming education." | |
messages = [sys_msg, HumanMessage(content=user_question)] | |
result = ninu.invoke({"messages": messages}) | |
for msg in result["messages"]: | |
print("\n", msg.content) | |