File size: 4,695 Bytes
f5e247b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import math
from typing import Optional
from langchain.agents import initialize_agent,Tool, AgentType
from langchain.memory import ConversationBufferMemory
from langchain_deepseek import ChatDeepSeek
from dotenv import load_dotenv
import os
from datetime import datetime

load_dotenv()
# 1. Initialize DeepSeek LLM
#    You may need to pass your DeepSeek API key or client config here.
llm = ChatDeepSeek(api_key=os.getenv("DEEPSEEK_API_KEY"), model="deepseek-chat")

def get_weather(location: str) -> str:
    # stub implementation—replace with real weather API call
    return f"Weather forecast for {location}: Sunny with a high of 25°C and low of 15°C. 10% chance of precipitation."

def calculator(operation_input: str) -> str:
    """Perform basic arithmetic operations from a text description."""
    try:
        # Parse the operation input
        parts = operation_input.split()
        
        # Try to identify the operation and numbers
        if "add" in operation_input.lower() or "+" in operation_input:
            # Look for two numbers in the string
            numbers = [float(s) for s in parts if s.replace('.', '', 1).isdigit()]
            if len(numbers) >= 2:
                return f"{numbers[0]} + {numbers[1]} = {numbers[0] + numbers[1]}"
            else:
                return "Error: Could not identify two numbers for addition"
            
        elif "subtract" in operation_input.lower() or "-" in operation_input:
            numbers = [float(s) for s in parts if s.replace('.', '', 1).isdigit()]
            if len(numbers) >= 2:
                return f"{numbers[0]} - {numbers[1]} = {numbers[0] - numbers[1]}"
            else:
                return "Error: Could not identify two numbers for subtraction"
            
        elif "multiply" in operation_input.lower() or "*" in operation_input:
            numbers = [float(s) for s in parts if s.replace('.', '', 1).isdigit()]
            if len(numbers) >= 2:
                return f"{numbers[0]} * {numbers[1]} = {numbers[0] * numbers[1]}"
            else:
                return "Error: Could not identify two numbers for multiplication"
            
        elif "divide" in operation_input.lower() or "/" in operation_input:
            numbers = [float(s) for s in parts if s.replace('.', '', 1).isdigit()]
            if len(numbers) >= 2:
                if numbers[1] == 0:
                    return "Error: Division by zero"
                return f"{numbers[0]} / {numbers[1]} = {numbers[0] / numbers[1]}"
            else:
                return "Error: Could not identify two numbers for division"
            
        elif "power" in operation_input.lower() or "^" in operation_input:
            numbers = [float(s) for s in parts if s.replace('.', '', 1).isdigit()]
            if len(numbers) >= 2:
                return f"{numbers[0]}^{numbers[1]} = {numbers[0] ** numbers[1]}"
            else:
                return "Error: Could not identify two numbers for power operation"
            
        elif "sqrt" in operation_input.lower():
            numbers = [float(s) for s in parts if s.replace('.', '', 1).isdigit()]
            if numbers:
                if numbers[0] < 0:
                    return "Error: Cannot take square root of negative number"
                return f"sqrt({numbers[0]}) = {math.sqrt(numbers[0])}"
            else:
                return "Error: Could not identify a number for square root"
        else:
            return f"Unknown operation. Please specify one of: add, subtract, multiply, divide, power, sqrt"
    except Exception as e:
        return f"Error performing calculation: {str(e)}"
    
tools = [
    Tool(
        name="Weather",
        func=get_weather,
        description="Get the current weather for a specified location."
    ),
    Tool(
        name="Calculator",
        func=calculator,
        description="Perform mathematical calculations. Available operations: add, subtract, multiply, divide, power, sqrt. For example: operation='add', a=2, b=3 will return '2 + 3 = 5'."
    ),
]

memory = ConversationBufferMemory(
    memory_key="chat_history",
    return_messages=True
)

agent_executor = initialize_agent(
    tools=tools,
    llm=llm,
    memory=memory,
    agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,
    verbose=True,
)

if __name__ == "__main__":
    while True:
        query = input("\nEnter your question (or 'exit' to quit): ")
        if query.lower() == 'exit':
            break
        result = agent_executor.invoke(query)
        print(f"Result: {result['output']}")