File size: 4,719 Bytes
5a40322
ec962d2
47b8bcf
 
 
 
 
 
 
 
 
 
 
ec962d2
47b8bcf
 
 
 
 
 
 
 
 
f269466
47b8bcf
 
5a40322
 
47b8bcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a40322
 
47b8bcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os
from dotenv import load_dotenv
from typing import List
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_experimental.tools import PythonREPLTool
from langchain_community.vectorstores import FAISS
#from langchain.embeddings import OpenAIEmbeddings
from langchain_core.documents import Document
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace, HuggingFaceEmbeddings
from langchain_core.messages import HumanMessage



load_dotenv()

# -----------------------------
# LLM
# -----------------------------
#llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
#repo_id="deepseek-ai/DeepSeek-V4-Pro"
llm = ChatHuggingFace(
    llm=HuggingFaceEndpoint(
        repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
        huggingfacehub_api_token='HF_KEY',
        task="conversational", # Specify task for the conversational model
    )
)

# -----------------------------
# Tools
# -----------------------------
search = DuckDuckGoSearchRun()
python_tool = PythonREPLTool()

TOOLS = {
    "search": search.run,
    "python": python_tool.run,
    "llm": lambda x: llm.invoke([HumanMessage(content=x)]).content,
    "summarize": lambda text: llm.invoke([HumanMessage(content=f"Summarize the following:\n{text}")]).content
}

# -----------------------------
# Memory (Vector DB)
# -----------------------------
embeddings = HuggingFaceEmbeddings()
# Initialize FAISS with a dummy document to prevent IndexError when trying to determine embedding dimension
vectorstore = FAISS.from_documents([Document(page_content="initialization_document_for_dimension_inference")], embeddings)

def store_memory(text: str):
    vectorstore.add_documents([Document(page_content=text)])

def retrieve_memory(query: str):
    docs = vectorstore.similarity_search(query, k=3)
    return "\n".join([d.page_content for d in docs])

# -----------------------------
# Planner
# -----------------------------
def plan(goal, history):
    prompt = f"""
You are an autonomous agent.

Goal: {goal}

Previous steps:
{history}

Decide the NEXT action:
- search(query)
- python(code)
- llm(prompt)
- summarize(text)
- finish(answer)

Respond ONLY in one line.
"""
    return llm.invoke([HumanMessage(content=prompt)]).content.strip()

# -----------------------------
# Executor
# -----------------------------
def execute(action: str):
    try:
        if action.startswith("search("):
            query = action[len("search("):-1]
            return TOOLS["search"](query)

        elif action.startswith("python("):
            code = action[len("python("):-1]
            return TOOLS["python"](code)

        elif action.startswith("llm("):
            prompt = action[len("llm("):-1]
            return TOOLS["llm"](prompt)

        elif action.startswith("summarize("):
            text_to_summarize = action[len("summarize("):-1]
            return TOOLS["summarize"](text_to_summarize)

        elif action.startswith("finish("):
            return action[len("finish("):-1]

        else:
            return "Invalid action"

    except Exception as e:
        return f"Error: {str(e)}"

# -----------------------------
# Critic (loop control)
# -----------------------------
def critic(goal, last_result):
    prompt = f"""
Goal: {goal}

Latest result:
{last_result}

Is the goal achieved? Answer YES or NO.
"""
    return "YES" in llm.invoke([HumanMessage(content=prompt)]).content.upper()

# -----------------------------
# Autonomous Loop
# -----------------------------
def autonomous_agent(goal: str, max_steps=15):

    history = ""
    print(f"\n🎯 Goal: {goal}\n")

    for step in range(max_steps):
        print(f"--- Step {step+1} ---")

        # Retrieve memory
        memory_context = retrieve_memory(goal)

        action = plan(goal, history + "\nMemory:\n" + memory_context)
        print(f"🧠 Plan: {action}")

        result = execute(action)
        print(f"⚙️ Result: {result[:300]}...\n")

        # Store memory
        store_memory(f"Action: {action}\nResult: {result}")

        history += f"\nStep {step+1}: {action}{result}"

        # Finish condition
        if action.startswith("finish("):
            print("✅ Finished by agent")
            return result

        # Critic check
        if critic(goal, result):
            print("✅ Critic determined goal achieved")
            return result

    return "❌ Max steps reached without completion"

# -----------------------------
# Run
# -----------------------------
if __name__ == "__main__":
    while True:
        goal = input("\nEnter goal (or 'exit'): ")
        if goal == "exit":
            break

        result = autonomous_agent(goal)
        print(f"\n🤖 Final Output:\n{result}\n")