Add Claude class for Anthropic API integration and update requirements
Browse files- app.py +21 -5
- requirements.txt +2 -1
app.py
CHANGED
@@ -12,9 +12,10 @@ from pathlib import Path
|
|
12 |
import openai
|
13 |
from openai import OpenAI
|
14 |
import pdfplumber
|
|
|
15 |
|
16 |
|
17 |
-
##
|
18 |
def is_image_extension(filename: str) -> bool:
|
19 |
IMAGE_EXTS = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp', '.svg'}
|
20 |
ext = os.path.splitext(filename)[1].lower() # os.path.splitext(path) returns (root, ext)
|
@@ -61,8 +62,19 @@ def check_format(answer: str | list, *args, **kwargs) -> list:
|
|
61 |
return [answer]
|
62 |
elif isinstance(answer, dict):
|
63 |
raise TypeError(f"Final answer must be a list, not a dict. Please check the answer format.")
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
## tools definition
|
67 |
@tool
|
68 |
def download_images(image_urls: str) -> list:
|
@@ -153,11 +165,15 @@ def generate_audio(prompt: str) -> object:
|
|
153 |
|
154 |
|
155 |
|
156 |
-
|
157 |
## agent definition
|
158 |
class Agent:
|
159 |
def __init__(self, ):
|
160 |
-
client = HfApiModel("
|
|
|
|
|
|
|
|
|
|
|
161 |
self.agent = CodeAgent(
|
162 |
model=client,
|
163 |
tools=[DuckDuckGoSearchTool(max_results=5), VisitWebpageTool(max_output_length=20000), generate_image, download_images, transcribe_audio],
|
|
|
12 |
import openai
|
13 |
from openai import OpenAI
|
14 |
import pdfplumber
|
15 |
+
import anthropic
|
16 |
|
17 |
|
18 |
+
## utilties and class definition
|
19 |
def is_image_extension(filename: str) -> bool:
|
20 |
IMAGE_EXTS = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp', '.svg'}
|
21 |
ext = os.path.splitext(filename)[1].lower() # os.path.splitext(path) returns (root, ext)
|
|
|
62 |
return [answer]
|
63 |
elif isinstance(answer, dict):
|
64 |
raise TypeError(f"Final answer must be a list, not a dict. Please check the answer format.")
|
65 |
+
|
66 |
+
class Claude:
|
67 |
+
def __init__(self):
|
68 |
+
self.client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
69 |
+
|
70 |
+
def generate(self, prompt: str):
|
71 |
+
message = self.client.messages.create(
|
72 |
+
model="claude-sonnet-4-20250514",
|
73 |
+
max_tokens=20000,
|
74 |
+
temperature=1,
|
75 |
+
messages=[{"role": "user", "content": prompt}]
|
76 |
+
)
|
77 |
+
return message.content
|
78 |
## tools definition
|
79 |
@tool
|
80 |
def download_images(image_urls: str) -> list:
|
|
|
165 |
|
166 |
|
167 |
|
|
|
168 |
## agent definition
|
169 |
class Agent:
|
170 |
def __init__(self, ):
|
171 |
+
#client = HfApiModel("Qwen/Qwen3-32B", provider="nebius", api_key=os.getenv("NEBIUS_API_KEY"))
|
172 |
+
client = OpenAIServerModel(
|
173 |
+
model_id="claude-sonnet-4-20250514",
|
174 |
+
api_base="https://api.anthropic.com/v1/",
|
175 |
+
api_key=os.environ["ANTHROPIC_API_KEY"],
|
176 |
+
)
|
177 |
self.agent = CodeAgent(
|
178 |
model=client,
|
179 |
tools=[DuckDuckGoSearchTool(max_results=5), VisitWebpageTool(max_output_length=20000), generate_image, download_images, transcribe_audio],
|
requirements.txt
CHANGED
@@ -3,4 +3,5 @@ smolagents
|
|
3 |
openai
|
4 |
gradio
|
5 |
duckduckgo-search
|
6 |
-
pdfplumber
|
|
|
|
3 |
openai
|
4 |
gradio
|
5 |
duckduckgo-search
|
6 |
+
pdfplumber
|
7 |
+
anthropic
|