Spaces:
Runtime error
Runtime error
Create agent.py
Browse files
agent.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from langchain import hub
|
3 |
+
from langchain.agents import initialize_agent, AgentType, Tool
|
4 |
+
from langchain.llms import HuggingFaceHub
|
5 |
+
from langchain_community.tools import DuckDuckGoSearchResults
|
6 |
+
from langchain_experimental.tools import PythonREPLTool
|
7 |
+
from langchain.agents.agent_toolkits import create_python_agent
|
8 |
+
from langchain.agents import load_tools
|
9 |
+
|
10 |
+
# Lade dein Hugging Face Token (falls benötigt)
|
11 |
+
from huggingface_hub import login
|
12 |
+
# login(token="your-huggingface-token-here") # Optional, falls dein Space das braucht
|
13 |
+
|
14 |
+
# LLM: Mistral-7B-Instruct über Hugging Face Inference API
|
15 |
+
llm = HuggingFaceHub(
|
16 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
17 |
+
model_kwargs={"temperature": 0.2, "max_new_tokens": 512}
|
18 |
+
)
|
19 |
+
|
20 |
+
# Tools definieren
|
21 |
+
search_tool = DuckDuckGoSearchResults()
|
22 |
+
python_tool = PythonREPLTool()
|
23 |
+
|
24 |
+
tools = [
|
25 |
+
Tool(
|
26 |
+
name="Search",
|
27 |
+
func=search_tool.run,
|
28 |
+
description="Useful for when you need to answer questions about current events or look up information online."
|
29 |
+
),
|
30 |
+
Tool(
|
31 |
+
name="Python_REPL",
|
32 |
+
func=python_tool.run,
|
33 |
+
description="Useful for math, calculations, or running simple python code."
|
34 |
+
),
|
35 |
+
]
|
36 |
+
|
37 |
+
# Agent initialisieren
|
38 |
+
agent_executor = initialize_agent(
|
39 |
+
tools,
|
40 |
+
llm,
|
41 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
42 |
+
verbose=True,
|
43 |
+
handle_parsing_errors=True,
|
44 |
+
)
|