File size: 3,409 Bytes
92ea5cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
from flask import Flask, request, jsonify
import os
from langchain.prompts import ChatPromptTemplate
from langchain.agents import Tool, create_openai_tools_agent, AgentExecutor
from langchain_core.prompts import MessagesPlaceholder
from skills.vision import get_chat_completion
from skills.serch import search_tool
from skills.wiki import wikipedia_tool
from skills.control_mode import manual_control_mode , deactivate_manual_control_mode
from langchain_groq import ChatGroq
import requests

# Initialize the Groq API key
GROQ_API_KEY = "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"  # Replace with your actual API key
os.environ["GROQ_API_KEY"] = GROQ_API_KEY

# Initialize the Groq LLM
llm = ChatGroq(model_name="mixtral-8x7b-32768", temperature=0.7, max_tokens=4096)


# ____________________tools section_____________________





def analyse(query):

    response = get_chat_completion(query, image_file)
    return response


# Initialize external tools
tools = [
    Tool(
        name="analyseimage",
        func=analyse,
        description="Tool for image vision LLM model queries",
    ),
    Tool(
        name="Search",
        func=search_tool,
        description="Search the internet for current information",
    ),
    Tool(
        name="Wikipedia",
        func=wikipedia_tool,
        description="Query Wikipedia for detailed topic information",
    ),
    Tool(
        name="Manual remote Control",
        func=manual_control_mode,
        description="if user said activate manual  remote control mode this function needs to trigger ",
    ),
    Tool(
        name="deativate manualControl",
        func=deactivate_manual_control_mode,
        description="if user said deactivate manual remote control mode this function needs to trigger ",
    ),
    
]

# Create the agent prompt
template_messages = [
    (
        "system",
        "You are an AI assistant capable of using tools to analyze images, search the internet, and query Wikipedia.",
    ),
    ("user", "{input}"),
    MessagesPlaceholder(variable_name="agent_scratchpad"),
]
prompt = ChatPromptTemplate.from_messages(template_messages)

# Create the agent
agent = create_openai_tools_agent(llm=llm, tools=tools, prompt=prompt)

# Create the agent executor
agent_executor = AgentExecutor(
    agent=agent, tools=tools, verbose=True, handle_parsing_errors=True
)

# Initialize Flask app
app = Flask(__name__)


@app.route("/analyze", methods=["POST"])
def analyze():
    global image_file
    global query
    try:
        query = request.form.get("query")
        if not query:
            return jsonify({"error": "Query is required"}), 400
        image_file = request.files.get("image")

        response = agent_executor.invoke({"input": query})

        return jsonify({"response": response["output"]})

    except Exception as e:
        return jsonify({"error": f"Server error: {str(e)}"}), 500


# CORS Headers
@app.after_request
def after_request(response):
    response.headers.add("Access-Control-Allow-Origin", "*")
    response.headers.add("Access-Control-Allow-Headers", "Content-Type")
    response.headers.add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS")
    return response


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=5000)