Spaces:
Running
Running
ffreemt
commited on
Commit
·
48ec86e
1
Parent(s):
c851f63
Fix agent.run, fallbacks somehow dont work
Browse files- .python-version +1 -0
- .ruff.toml +20 -0
- __pycache__/exit_gracefully.cpython-312.pyc +0 -0
- __pycache__/get_gemini_keys.cpython-312.pyc +0 -0
- __pycache__/get_model.cpython-312.pyc +0 -0
- __pycache__/litellm_model.cpython-312.pyc +0 -0
- __pycache__/openai_model.cpython-312.pyc +0 -0
- basic_agent.py +58 -8
- exit_gracefully.py +11 -0
- get_gemini_keys.py +6 -1
- get_model.py +20 -6
- litellm_model.py +89 -0
- main.py +6 -0
- openai_model.py +76 -0
- tasks.py +51 -0
- ty.toml +2 -0
- uv.lock +8 -0
.python-version
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
3.12
|
.ruff.toml
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Assume Python 3.12.
|
2 |
+
target-version = "py312"
|
3 |
+
# Decrease the maximum line length to 79 characters.
|
4 |
+
line-length = 300
|
5 |
+
|
6 |
+
# pyflakes, pycodestyle, isort
|
7 |
+
# flake8 YTT, pydocstyle D, pylint PLC
|
8 |
+
lint.select = ["F", "E", "W", "I001", "YTT", "D", "PLC"]
|
9 |
+
# select = ["ALL"]
|
10 |
+
|
11 |
+
# D100 Missing docstring in public module
|
12 |
+
# D101 Missing docstring in public class
|
13 |
+
# D102 D102 Missing docstring in public method
|
14 |
+
# D103 Missing docstring in public function
|
15 |
+
# D107 Missing docstring in `__init__`
|
16 |
+
# `multi-line-summary-first-line` (D212)
|
17 |
+
# `one-blank-line-before-class` (D203)
|
18 |
+
lint.extend-ignore = ["D100", "D101", "D102", "D103", "D107", "D212", "D203"]
|
19 |
+
|
20 |
+
exclude = [".venv"]
|
__pycache__/exit_gracefully.cpython-312.pyc
ADDED
Binary file (550 Bytes). View file
|
|
__pycache__/get_gemini_keys.cpython-312.pyc
CHANGED
Binary files a/__pycache__/get_gemini_keys.cpython-312.pyc and b/__pycache__/get_gemini_keys.cpython-312.pyc differ
|
|
__pycache__/get_model.cpython-312.pyc
CHANGED
Binary files a/__pycache__/get_model.cpython-312.pyc and b/__pycache__/get_model.cpython-312.pyc differ
|
|
__pycache__/litellm_model.cpython-312.pyc
ADDED
Binary file (2.57 kB). View file
|
|
__pycache__/openai_model.cpython-312.pyc
ADDED
Binary file (2.32 kB). View file
|
|
basic_agent.py
CHANGED
@@ -9,9 +9,13 @@ import pandas as pd
|
|
9 |
import requests
|
10 |
import rich
|
11 |
import smolagents
|
12 |
-
|
13 |
from loguru import logger
|
14 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, HfApiModel, VisitWebpageTool
|
|
|
|
|
|
|
|
|
15 |
|
16 |
print = rich.get_console().print
|
17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
@@ -45,10 +49,33 @@ AUTHORIZED_IMPORTS = [
|
|
45 |
]
|
46 |
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
@dataclass
|
49 |
class BasicAgent:
|
50 |
model: smolagents.models.Model = HfApiModel()
|
51 |
tools: list = field(default_factory=lambda: [])
|
|
|
52 |
# def __init__(self):
|
53 |
def __post_init__(self):
|
54 |
"""Run post_init."""
|
@@ -56,7 +83,7 @@ class BasicAgent:
|
|
56 |
self.agent = CodeAgent(
|
57 |
tools=self.tools,
|
58 |
model=self.model,
|
59 |
-
verbosity_level=
|
60 |
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
61 |
planning_interval=4,
|
62 |
)
|
@@ -66,17 +93,17 @@ class BasicAgent:
|
|
66 |
|
67 |
def __call__(self, question: str) -> str:
|
68 |
# print(f"Agent received question (first 50 chars): {question[:50]}...")
|
69 |
-
print(f"Agent received question: {question}...")
|
70 |
|
71 |
# fixed_answer = "This is a default answer."
|
72 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
73 |
# return fixed_answer
|
74 |
try:
|
75 |
# answer = self.get_answer(question)
|
76 |
-
answer = self.agent(question)
|
77 |
except Exception as e:
|
78 |
logger.error(e)
|
79 |
-
answer = str(e)[:
|
80 |
|
81 |
return answer
|
82 |
|
@@ -93,7 +120,28 @@ def main():
|
|
93 |
|
94 |
space_id = f"{username}/{repo_name}"
|
95 |
|
96 |
-
model = get_model(cat="gemini")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
98 |
try:
|
99 |
# agent = BasicAgent()
|
@@ -102,6 +150,7 @@ def main():
|
|
102 |
tools=[
|
103 |
DuckDuckGoSearchTool(),
|
104 |
VisitWebpageTool(),
|
|
|
105 |
FinalAnswerTool(),
|
106 |
]
|
107 |
)
|
@@ -142,7 +191,8 @@ def main():
|
|
142 |
print(f"Running agent on {len(questions_data)} questions...")
|
143 |
|
144 |
# for item in questions_data:
|
145 |
-
for item in questions_data[-1:]:
|
|
|
146 |
task_id = item.get("task_id")
|
147 |
question_text = item.get("question")
|
148 |
if not task_id or question_text is None:
|
|
|
9 |
import requests
|
10 |
import rich
|
11 |
import smolagents
|
12 |
+
import wikipediaapi
|
13 |
from loguru import logger
|
14 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, HfApiModel, Tool, VisitWebpageTool
|
15 |
+
|
16 |
+
from get_model import get_model
|
17 |
+
from litellm_model import litellm_model
|
18 |
+
from openai_model import openai_model
|
19 |
|
20 |
print = rich.get_console().print
|
21 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
49 |
]
|
50 |
|
51 |
|
52 |
+
class WikipediaSearchTool(Tool):
|
53 |
+
name = "wikipedia_search"
|
54 |
+
description = "Fetches a summary of a Wikipedia page based on a given search query (only one word or group of words)."
|
55 |
+
|
56 |
+
inputs = {
|
57 |
+
"query": {"type": "string", "description": "The search term for the Wikipedia page (only one word or group of words)."}
|
58 |
+
}
|
59 |
+
|
60 |
+
output_type = "string"
|
61 |
+
|
62 |
+
def __init__(self, lang="en"):
|
63 |
+
super().__init__()
|
64 |
+
self.wiki = wikipediaapi.Wikipedia(
|
65 |
+
language=lang, user_agent="MinimalAgent/1.0")
|
66 |
+
|
67 |
+
def forward(self, query: str):
|
68 |
+
page = self.wiki.page(query)
|
69 |
+
if not page.exists():
|
70 |
+
return "No Wikipedia page found."
|
71 |
+
return page.summary[:1000]
|
72 |
+
|
73 |
+
|
74 |
@dataclass
|
75 |
class BasicAgent:
|
76 |
model: smolagents.models.Model = HfApiModel()
|
77 |
tools: list = field(default_factory=lambda: [])
|
78 |
+
verbosity_level: int = 0
|
79 |
# def __init__(self):
|
80 |
def __post_init__(self):
|
81 |
"""Run post_init."""
|
|
|
83 |
self.agent = CodeAgent(
|
84 |
tools=self.tools,
|
85 |
model=self.model,
|
86 |
+
verbosity_level=self.verbosity_level,
|
87 |
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
88 |
planning_interval=4,
|
89 |
)
|
|
|
93 |
|
94 |
def __call__(self, question: str) -> str:
|
95 |
# print(f"Agent received question (first 50 chars): {question[:50]}...")
|
96 |
+
# print(f"Agent received question: {question}...")
|
97 |
|
98 |
# fixed_answer = "This is a default answer."
|
99 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
100 |
# return fixed_answer
|
101 |
try:
|
102 |
# answer = self.get_answer(question)
|
103 |
+
answer = self.agent.run(question)
|
104 |
except Exception as e:
|
105 |
logger.error(e)
|
106 |
+
answer = str(e)[:110] + "..."
|
107 |
|
108 |
return answer
|
109 |
|
|
|
120 |
|
121 |
space_id = f"{username}/{repo_name}"
|
122 |
|
123 |
+
# model = get_model(cat="gemini")
|
124 |
+
|
125 |
+
_ = (
|
126 |
+
"gemini-2.5-flash-preview-04-17",
|
127 |
+
# "https://api-proxy.me/gemini/v1beta",
|
128 |
+
"https://generativelanguage.googleapis.com/v1beta",
|
129 |
+
os.getenv("GEMINI_API_KEY"),
|
130 |
+
)
|
131 |
+
|
132 |
+
_ = (
|
133 |
+
"grok-3-beta",
|
134 |
+
"https://api.x.ai/v1",
|
135 |
+
os.getenv("XAI_API_KEY"),
|
136 |
+
)
|
137 |
+
|
138 |
+
# model = litellm_model(*_)
|
139 |
+
model = openai_model(*_)
|
140 |
+
|
141 |
+
messages = [{'role': 'user', 'content': 'Say this is a test.'}]
|
142 |
+
print(model(messages))
|
143 |
+
# raise SystemExit("By intention")
|
144 |
+
|
145 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
146 |
try:
|
147 |
# agent = BasicAgent()
|
|
|
150 |
tools=[
|
151 |
DuckDuckGoSearchTool(),
|
152 |
VisitWebpageTool(),
|
153 |
+
WikipediaSearchTool(),
|
154 |
FinalAnswerTool(),
|
155 |
]
|
156 |
)
|
|
|
191 |
print(f"Running agent on {len(questions_data)} questions...")
|
192 |
|
193 |
# for item in questions_data:
|
194 |
+
# for item in questions_data[-1:]:
|
195 |
+
for item in questions_data[14:15]:
|
196 |
task_id = item.get("task_id")
|
197 |
question_text = item.get("question")
|
198 |
if not task_id or question_text is None:
|
exit_gracefully.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# pylint: disable=unused-argument,missing-module-docstring,missing-function-docstring
|
2 |
+
import signal
|
3 |
+
import sys
|
4 |
+
|
5 |
+
|
6 |
+
def exit_gracefully(signum, frame):
|
7 |
+
print("Exiting...")
|
8 |
+
sys.exit(0)
|
9 |
+
|
10 |
+
|
11 |
+
signal.signal(signal.SIGINT, exit_gracefully)
|
get_gemini_keys.py
CHANGED
@@ -5,10 +5,15 @@ from pathlib import Path
|
|
5 |
import rich
|
6 |
import yaml
|
7 |
from dotenv import dotenv_values
|
|
|
8 |
|
9 |
|
10 |
def get_gemini_keys(file=r".env-gemini", dotenv=False):
|
11 |
"""Get gemini keys."""
|
|
|
|
|
|
|
|
|
12 |
if Path(file).name.startswith(".env"):
|
13 |
dotenv = True
|
14 |
|
@@ -19,7 +24,7 @@ def get_gemini_keys(file=r".env-gemini", dotenv=False):
|
|
19 |
try:
|
20 |
keys = yaml.load(dotenv_values(file).get("GEMINI_API_KEYS"), yaml.Loader)
|
21 |
except Exception as e:
|
22 |
-
|
23 |
return []
|
24 |
return keys
|
25 |
|
|
|
5 |
import rich
|
6 |
import yaml
|
7 |
from dotenv import dotenv_values
|
8 |
+
from loguru import logger
|
9 |
|
10 |
|
11 |
def get_gemini_keys(file=r".env-gemini", dotenv=False):
|
12 |
"""Get gemini keys."""
|
13 |
+
if not Path(file).exists():
|
14 |
+
logger.debug(f"{file} does not exit, returing [] ")
|
15 |
+
return []
|
16 |
+
|
17 |
if Path(file).name.startswith(".env"):
|
18 |
dotenv = True
|
19 |
|
|
|
24 |
try:
|
25 |
keys = yaml.load(dotenv_values(file).get("GEMINI_API_KEYS"), yaml.Loader)
|
26 |
except Exception as e:
|
27 |
+
logger.error(e)
|
28 |
return []
|
29 |
return keys
|
30 |
|
get_model.py
CHANGED
@@ -25,7 +25,7 @@ def get_model(cat: str = "hf", provider=None, model_id=None):
|
|
25 |
# get gemini_api_keys
|
26 |
# dedup
|
27 |
_ = re.findall(r"AIzaSy[A-Z][\w-]{32}", os.getenv("GEMINI_API_KEYS", ""))
|
28 |
-
gemini_api_keys = dict.fromkeys(get_gemini_keys() + _)
|
29 |
|
30 |
# assert gemini_api_keys, "No GEMINI_API_KEYS, set env var GEMINI_API_KEYS or put them in .env-gemini and try again."
|
31 |
if not gemini_api_keys:
|
@@ -67,23 +67,37 @@ def get_model(cat: str = "hf", provider=None, model_id=None):
|
|
67 |
"api_key": os.getenv("SILICONFLOW_API_KEY"),
|
68 |
"api_base": "https://api.siliconflow.cn/v1",
|
69 |
},
|
70 |
-
}
|
71 |
]
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
fallbacks = []
|
74 |
model_list = llm_loadbalancer_model_list_gemini
|
75 |
if os.getenv("SILICONFLOW_API_KEY"):
|
76 |
fallbacks = [{"model-group-1": "model-group-2"}]
|
77 |
model_list += llm_loadbalancer_model_list_siliconflow
|
78 |
-
|
|
|
|
|
|
|
|
|
79 |
model = LiteLLMRouterModel(
|
80 |
model_id="model-group-1",
|
81 |
model_list=model_list,
|
82 |
client_kwargs={
|
83 |
"routing_strategy": "simple-shuffle",
|
84 |
"num_retries": 3,
|
85 |
-
|
86 |
-
"fallbacks":
|
87 |
},
|
88 |
)
|
89 |
|
|
|
25 |
# get gemini_api_keys
|
26 |
# dedup
|
27 |
_ = re.findall(r"AIzaSy[A-Z][\w-]{32}", os.getenv("GEMINI_API_KEYS", ""))
|
28 |
+
gemini_api_keys = [*dict.fromkeys(get_gemini_keys() + _)]
|
29 |
|
30 |
# assert gemini_api_keys, "No GEMINI_API_KEYS, set env var GEMINI_API_KEYS or put them in .env-gemini and try again."
|
31 |
if not gemini_api_keys:
|
|
|
67 |
"api_key": os.getenv("SILICONFLOW_API_KEY"),
|
68 |
"api_base": "https://api.siliconflow.cn/v1",
|
69 |
},
|
70 |
+
},
|
71 |
]
|
72 |
+
|
73 |
+
# gemma-3-27b-it
|
74 |
+
llm_loadbalancer_model_list_gemma = [
|
75 |
+
{
|
76 |
+
"model_name": "model-group-3",
|
77 |
+
"litellm_params": {
|
78 |
+
"model": f"gemini/gemma-3-27b-it",
|
79 |
+
"api_key": os.getenv("GEMINI_API_KEY") },
|
80 |
+
},
|
81 |
+
]
|
82 |
+
|
83 |
fallbacks = []
|
84 |
model_list = llm_loadbalancer_model_list_gemini
|
85 |
if os.getenv("SILICONFLOW_API_KEY"):
|
86 |
fallbacks = [{"model-group-1": "model-group-2"}]
|
87 |
model_list += llm_loadbalancer_model_list_siliconflow
|
88 |
+
|
89 |
+
model_list += llm_loadbalancer_model_list_gemma
|
90 |
+
fallbacks13 = [{"model-group-1": "model-group-3"}]
|
91 |
+
fallbacks31 = [{"model-group-3": "model-group-1"}]
|
92 |
+
|
93 |
model = LiteLLMRouterModel(
|
94 |
model_id="model-group-1",
|
95 |
model_list=model_list,
|
96 |
client_kwargs={
|
97 |
"routing_strategy": "simple-shuffle",
|
98 |
"num_retries": 3,
|
99 |
+
"retry_after": 180, # waits min s before retrying request
|
100 |
+
"fallbacks": fallbacks13, # falllacks dont seem to work
|
101 |
},
|
102 |
)
|
103 |
|
litellm_model.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ruff: noqa: F401
|
2 |
+
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
|
6 |
+
import rich
|
7 |
+
from loguru import logger
|
8 |
+
from smolagents import LiteLLMModel, OpenAIServerModel
|
9 |
+
|
10 |
+
from exit_gracefully import exit_gracefully
|
11 |
+
|
12 |
+
print = rich.get_console().print # noqa
|
13 |
+
|
14 |
+
|
15 |
+
def litellm_model(
|
16 |
+
model_id = None,
|
17 |
+
api_base = None,
|
18 |
+
api_key = None,
|
19 |
+
provider = "openai",
|
20 |
+
kwargs = None,
|
21 |
+
):
|
22 |
+
kwargs = kwargs or {}
|
23 |
+
# default siliconflow
|
24 |
+
api_base = api_base or "https://api.siliconflow.cn/v1"
|
25 |
+
api_key = api_key or os.getenv("SILICONFLOW_API_KEY")
|
26 |
+
model_id = model_id or "deepseek-ai/DeepSeek-V3"
|
27 |
+
|
28 |
+
if provider.lower() in ["gemini", "xai"]:
|
29 |
+
return LiteLLMModel(
|
30 |
+
f"{provider}/{model_id}",
|
31 |
+
# api_base=api_base, # must not set api_base to None or not set at all for gemini
|
32 |
+
api_key=api_key,
|
33 |
+
temperature=0.,
|
34 |
+
**kwargs,
|
35 |
+
)
|
36 |
+
else:
|
37 |
+
return LiteLLMModel(
|
38 |
+
f"{provider}/{model_id}",
|
39 |
+
api_base=api_base,
|
40 |
+
api_key=api_key,
|
41 |
+
temperature=0.,
|
42 |
+
**kwargs,
|
43 |
+
)
|
44 |
+
|
45 |
+
|
46 |
+
def main():
|
47 |
+
messages = [{'role': 'user', 'content': 'Say this is a test.'}]
|
48 |
+
logger.debug(sys.argv)
|
49 |
+
if not sys.argv[1:]:
|
50 |
+
model = litellm_model()
|
51 |
+
logger.debug(model(messages))
|
52 |
+
return
|
53 |
+
|
54 |
+
if len(sys.argv[1:]) < 4:
|
55 |
+
raise SystemExit("Provide at least four args (model_id, api_base, api_key, provider)")
|
56 |
+
|
57 |
+
model_id, api_base, api_key, provider, *_ = sys.argv[1:]
|
58 |
+
model = litellm_model(model_id, api_base, api_key, provider)
|
59 |
+
try:
|
60 |
+
response = model(messages)
|
61 |
+
logger.debug(response)
|
62 |
+
except Exception as e:
|
63 |
+
logger.error(e)
|
64 |
+
return
|
65 |
+
try:
|
66 |
+
print(response.content)
|
67 |
+
except Exception as e:
|
68 |
+
logger.error(e)
|
69 |
+
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
main()
|
73 |
+
# python litellm_model.py
|
74 |
+
# deepseek-ai/DeepSeek-V3 https://api.siliconflow.cn/v1 %SILICONFLOW_API_KEY%
|
75 |
+
|
76 |
+
# python litellm_model.py grok-3-beta https://api.x.ai/v1 %XAI_API_KEY%
|
77 |
+
|
78 |
+
# set proxy or use https://api-proxy.me/gemini/v1beta
|
79 |
+
# gemini-2.5-flash-preview-04-17 https://generativelanguage.googleapis.com/v1beta %GEMINI_API_KEY%
|
80 |
+
# python litellm_model.py gemini-2.5-flash-preview-04-17 https://api-proxy.me/gemini/v1beta %GEMINI_API_KEY%
|
81 |
+
# gemini-2.0-flash
|
82 |
+
# litellmmodel
|
83 |
+
|
84 |
+
# https://api.together.ai/models/deepseek-ai/DeepSeek-V3
|
85 |
+
# deepseek-ai/DeepSeek-V3 https://api.together.xyz/v1 %TOGETHER_API_KEY%
|
86 |
+
|
87 |
+
# deepseek-chat https://litellm.dattw.eu.org/v1 %LITELLM_API_KEY%
|
88 |
+
|
89 |
+
# LLM API proxy: https://linux.do/t/topic/290871
|
main.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def main():
|
2 |
+
print("Hello from final-assignment-hf-agents-course!")
|
3 |
+
|
4 |
+
|
5 |
+
if __name__ == "__main__":
|
6 |
+
main()
|
openai_model.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ruff: noqa: F401
|
2 |
+
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
|
6 |
+
from exit_gracefully import exit_gracefully
|
7 |
+
from loguru import logger
|
8 |
+
from smolagents import LiteLLMModel, OpenAIServerModel
|
9 |
+
import rich
|
10 |
+
|
11 |
+
print = rich.get_console().print # noqa
|
12 |
+
|
13 |
+
|
14 |
+
def openai_model(
|
15 |
+
model_id = None,
|
16 |
+
api_base = None,
|
17 |
+
api_key = None,
|
18 |
+
kwargs = None,
|
19 |
+
):
|
20 |
+
kwargs = kwargs or {}
|
21 |
+
# default siliconflow
|
22 |
+
api_base = api_base or "https://api.siliconflow.cn/v1"
|
23 |
+
api_key = api_key or os.getenv("SILICONFLOW_API_KEY")
|
24 |
+
model_id = model_id or "deepseek-ai/DeepSeek-V3"
|
25 |
+
|
26 |
+
return OpenAIServerModel(
|
27 |
+
model_id,
|
28 |
+
api_base=api_base,
|
29 |
+
api_key=api_key,
|
30 |
+
temperature=0.,
|
31 |
+
**kwargs,
|
32 |
+
)
|
33 |
+
|
34 |
+
def main():
|
35 |
+
messages = [{'role': 'user', 'content': 'Say this is a test.'}]
|
36 |
+
logger.debug(sys.argv)
|
37 |
+
if not sys.argv[1:]:
|
38 |
+
model = openai_model()
|
39 |
+
logger.debug(model(messages))
|
40 |
+
return
|
41 |
+
|
42 |
+
if len(sys.argv[1:]) < 3:
|
43 |
+
raise SystemExit("Provide at least three args (model_id, api_base, api_key)")
|
44 |
+
|
45 |
+
model_id, api_base, api_key, *_ = sys.argv[1:]
|
46 |
+
model = openai_model(model_id, api_base, api_key)
|
47 |
+
try:
|
48 |
+
response = model(messages)
|
49 |
+
logger.debug(response)
|
50 |
+
except Exception as e:
|
51 |
+
logger.error(e)
|
52 |
+
return
|
53 |
+
try:
|
54 |
+
print(response.content)
|
55 |
+
except Exception as e:
|
56 |
+
logger.error(e)
|
57 |
+
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
main()
|
61 |
+
# python openai_model.py
|
62 |
+
# deepseek-ai/DeepSeek-V3 https://api.siliconflow.cn/v1 %SILICONFLOW_API_KEY%
|
63 |
+
|
64 |
+
# python openai_model.py grok-3-beta https://api.x.ai/v1 %XAI_API_KEY%
|
65 |
+
|
66 |
+
# gemini-2.5-flash-preview-04-17 https://generativelanguage.googleapis.com/v1beta %GEMINI_API_KEY%
|
67 |
+
# gemini-2.0-flash
|
68 |
+
|
69 |
+
# https://api.together.ai/models/deepseek-ai/DeepSeek-V3
|
70 |
+
# deepseek-ai/DeepSeek-V3 https://api.together.xyz/v1 %TOGETHER_API_KEY%
|
71 |
+
|
72 |
+
# deepseek-chat https://litellm.dattw.eu.org/v1 %LITELLM_API_KEY%
|
73 |
+
|
74 |
+
#
|
75 |
+
|
76 |
+
# LLM API proxy: https://linux.do/t/topic/290871
|
tasks.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Define invoke tasks.
|
3 |
+
|
4 |
+
invoke list
|
5 |
+
invoke build
|
6 |
+
|
7 |
+
invoke --help build
|
8 |
+
|
9 |
+
"""
|
10 |
+
from invoke import task
|
11 |
+
|
12 |
+
NAME_DEF = "final assignment tasks"
|
13 |
+
|
14 |
+
@task(
|
15 |
+
default=True,
|
16 |
+
)
|
17 |
+
def gemini(c):
|
18 |
+
"""Run python litellm_model.py gemini params."""
|
19 |
+
c.run("python litellm_model.py gemini-2.5-flash-preview-04-17 https://generativelanguage.googleapis.com/v1beta %GEMINI_API_KEY% gemini")
|
20 |
+
|
21 |
+
@task
|
22 |
+
def gemini1(c):
|
23 |
+
"""Run python litellm_model.py gemini-as-openai params."""
|
24 |
+
c.run("python litellm_model.py gemini-2.5-flash-preview-04-17 https://generativelanguage.googleapis.com/v1beta %GEMINI_API_KEY% openai")
|
25 |
+
|
26 |
+
@task
|
27 |
+
def gemini2(c):
|
28 |
+
"""Run python litellm_model.py gemini-via-proxy params."""
|
29 |
+
# c.run("python litellm_model.py gemini-2.5-flash-preview-04-17 https://generativelanguage.googleapis.com/v1beta %GEMINI_API_KEY%")
|
30 |
+
c.run("python litellm_model.py gemini-2.5-flash-preview-04-17 https://api-proxy.me/gemini/v1beta %GEMINI_API_KEY% openai")
|
31 |
+
|
32 |
+
@task
|
33 |
+
def xai(c):
|
34 |
+
"""Run python litellm_model.py grok params."""
|
35 |
+
c.run("python litellm_model.py grok-3-beta https://api.x.ai/v1 %XAI_API_KEY% xai")
|
36 |
+
|
37 |
+
@task
|
38 |
+
def grok1(c):
|
39 |
+
"""Run python litellm_model.py grok params."""
|
40 |
+
c.run("python litellm_model.py grok-3-beta https://api.x.ai/v1 %XAI_API_KEY% openai")
|
41 |
+
|
42 |
+
@task
|
43 |
+
def siliconflow(c):
|
44 |
+
"""Run python litellm_model.py (siliconflow setup)."""
|
45 |
+
c.run("python litellm_model.py ")
|
46 |
+
|
47 |
+
|
48 |
+
@task
|
49 |
+
def agent(c):
|
50 |
+
"""Run python basic_agent.py."""
|
51 |
+
c.run("python basic_agent.py ")
|
ty.toml
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
[rules]
|
2 |
+
unresolved-import = "ignore"
|
uv.lock
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
version = 1
|
2 |
+
revision = 2
|
3 |
+
requires-python = ">=3.12"
|
4 |
+
|
5 |
+
[[package]]
|
6 |
+
name = "final-assignment-hf-agents-course"
|
7 |
+
version = "0.1.0"
|
8 |
+
source = { virtual = "." }
|