alfulanny commited on
Commit
c767d20
·
verified ·
1 Parent(s): ab37407

Create code_agent.py

Browse files
Files changed (1) hide show
  1. code_agent.py +137 -0
code_agent.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Real `smolagents` CodeAgent integration for the final project.
3
+
4
+ This module initializes a `CodeAgent` with standard tools and exposes a
5
+ `run_agent(prompt)` function returning the final answer string.
6
+
7
+ Notes:
8
+ - Requires `smolagents` installed in the environment.
9
+ - For serverless inference via Hugging Face, you must be logged in
10
+ (`huggingface-cli login`) or have `HF_TOKEN` environment variable set,
11
+ and have sufficient provider credits.
12
+ """
13
+ from typing import List, Any
14
+ import logging
15
+ import os
16
+
17
+ logger = logging.getLogger(__name__)
18
+ logging.basicConfig(level=logging.INFO)
19
+
20
+ from smolagents import (
21
+ CodeAgent,
22
+ InferenceClientModel,
23
+ DuckDuckGoSearchTool,
24
+ FinalAnswerTool,
25
+ VisitWebpageTool,
26
+ )
27
+
28
+ # Get HF token from environment (set by huggingface-cli login or HF_TOKEN env var)
29
+ HF_TOKEN = os.environ.get("HF_TOKEN")
30
+ if not HF_TOKEN:
31
+ logger.warning(
32
+ "HF_TOKEN not found in environment. Run 'huggingface-cli login' or set HF_TOKEN env var. "
33
+ "CodeAgent initialization will fail without valid credentials."
34
+ )
35
+
36
+
37
+ def make_code_agent(
38
+ max_steps: int = 8,
39
+ verbosity: int = 1,
40
+ model_name: str | None = None,
41
+ ):
42
+ """Create and return a smolagents CodeAgent configured with standard tools.
43
+
44
+ Args:
45
+ max_steps: max reasoning steps for the agent.
46
+ verbosity: logging/output verbosity level.
47
+ model_name: HF model ID for serverless inference (e.g., 'allenai/Olmo-3-7B-Instruct').
48
+ If None, uses the default InferenceClientModel.
49
+
50
+ Returns:
51
+ CodeAgent instance.
52
+
53
+ Raises:
54
+ Exception: if InferenceClientModel initialization fails (missing HF login/credits).
55
+ """
56
+ tools: List[Any] = []
57
+
58
+ # Standard tools from smolagents
59
+ try:
60
+ tools.append(DuckDuckGoSearchTool())
61
+ except Exception as e:
62
+ logger.debug("DuckDuckGoSearchTool unavailable: %s", e)
63
+
64
+ try:
65
+ tools.append(VisitWebpageTool())
66
+ except Exception as e:
67
+ logger.debug("VisitWebpageTool unavailable: %s", e)
68
+
69
+ try:
70
+ tools.append(FinalAnswerTool())
71
+ except Exception as e:
72
+ logger.debug("FinalAnswerTool unavailable: %s", e)
73
+
74
+ # Initialize serverless inference model
75
+ try:
76
+ if model_name:
77
+ try:
78
+ model = InferenceClientModel(repo_id=model_name, token=HF_TOKEN)
79
+ except Exception:
80
+ try:
81
+ model = InferenceClientModel(model_id=model_name, token=HF_TOKEN)
82
+ except Exception:
83
+ model = InferenceClientModel(token=HF_TOKEN)
84
+ else:
85
+ model = InferenceClientModel(token=HF_TOKEN)
86
+ logger.info("InferenceClientModel initialized successfully with HF_TOKEN")
87
+ except Exception as e:
88
+ logger.error(
89
+ "InferenceClientModel initialization failed (ensure HF_TOKEN is set and has credits): %s", e
90
+ )
91
+ raise
92
+
93
+ agent = CodeAgent(tools=tools, model=model, max_steps=max_steps, verbosity_level=verbosity)
94
+ return agent
95
+
96
+
97
+ _AGENT_SINGLETON = None
98
+
99
+
100
+ def get_agent():
101
+ """Get or create the singleton CodeAgent instance."""
102
+ global _AGENT_SINGLETON
103
+ if _AGENT_SINGLETON is None:
104
+ _AGENT_SINGLETON = make_code_agent(model_name="allenai/Olmo-3-7B-Think")
105
+ return _AGENT_SINGLETON
106
+
107
+
108
+ def run_agent(prompt: str) -> str:
109
+ """Run the CodeAgent and return the final answer string.
110
+
111
+ Args:
112
+ prompt: the reasoning task/question for the agent.
113
+
114
+ Returns:
115
+ The agent's final answer as a string.
116
+
117
+ Raises:
118
+ Exception: if CodeAgent.run fails (e.g., no HF credentials or credits).
119
+ """
120
+ agent = get_agent()
121
+ res = agent.run(prompt)
122
+
123
+ # Normalize response to string
124
+ if isinstance(res, dict):
125
+ for key in ("answer", "final_answer", "final"):
126
+ if key in res and isinstance(res[key], str):
127
+ return res[key].strip()
128
+ return str(res)
129
+
130
+ if isinstance(res, str):
131
+ return res.strip()
132
+
133
+ return str(res)
134
+
135
+
136
+ if __name__ == "__main__":
137
+ print(run_agent("Give me a short list of 3 fruits."))