Commit ·
b5ed0c0
1
Parent(s): ba12808
updated claude server model
Browse files
app.py
CHANGED
|
@@ -19,12 +19,34 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 19 |
# --- Basic Agent Definition ---
|
| 20 |
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
| 21 |
class ClaudeServerModel:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def __init__(self, api_key: str, model_id: str = "claude-3-opus-20240229", temperature: float = 0.0):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
self.api_key = api_key
|
| 24 |
self.model_id = model_id
|
| 25 |
self.temperature = temperature
|
| 26 |
|
| 27 |
def complete(self, prompt: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
headers = {
|
| 29 |
"x-api-key": self.api_key,
|
| 30 |
"anthropic-version": "2023-06-01",
|
|
@@ -34,13 +56,26 @@ class ClaudeServerModel:
|
|
| 34 |
"model": self.model_id,
|
| 35 |
"max_tokens": 1024,
|
| 36 |
"temperature": self.temperature,
|
| 37 |
-
"messages": [
|
| 38 |
-
{"role": "user", "content": prompt}
|
| 39 |
-
]
|
| 40 |
}
|
| 41 |
response = requests.post("https://api.anthropic.com/v1/messages", headers=headers, json=body)
|
| 42 |
response.raise_for_status()
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# --- Constants ---
|
| 46 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 19 |
# --- Basic Agent Definition ---
|
| 20 |
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
| 21 |
class ClaudeServerModel:
|
| 22 |
+
"""
|
| 23 |
+
ClaudeServerModel acts as a wrapper to make Anthropic Claude models callable
|
| 24 |
+
in agentic frameworks like smolagents.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
def __init__(self, api_key: str, model_id: str = "claude-3-opus-20240229", temperature: float = 0.0):
|
| 28 |
+
"""
|
| 29 |
+
Initialize the ClaudeServerModel.
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
api_key (str): Your Claude API key.
|
| 33 |
+
model_id (str): Claude model ID (e.g., "claude-3-opus-20240229").
|
| 34 |
+
temperature (float): Sampling temperature for completion generation.
|
| 35 |
+
"""
|
| 36 |
self.api_key = api_key
|
| 37 |
self.model_id = model_id
|
| 38 |
self.temperature = temperature
|
| 39 |
|
| 40 |
def complete(self, prompt: str) -> str:
|
| 41 |
+
"""
|
| 42 |
+
Sends a prompt to the Claude API and returns the text response.
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
prompt (str): Prompt to send to the Claude model.
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
str: The Claude model's response text.
|
| 49 |
+
"""
|
| 50 |
headers = {
|
| 51 |
"x-api-key": self.api_key,
|
| 52 |
"anthropic-version": "2023-06-01",
|
|
|
|
| 56 |
"model": self.model_id,
|
| 57 |
"max_tokens": 1024,
|
| 58 |
"temperature": self.temperature,
|
| 59 |
+
"messages": [{"role": "user", "content": prompt}]
|
|
|
|
|
|
|
| 60 |
}
|
| 61 |
response = requests.post("https://api.anthropic.com/v1/messages", headers=headers, json=body)
|
| 62 |
response.raise_for_status()
|
| 63 |
+
response_data = response.json()
|
| 64 |
+
if "content" in response_data and isinstance(response_data["content"], list):
|
| 65 |
+
return response_data["content"][0].get("text", "").strip()
|
| 66 |
+
return "Claude API did not return valid text content."
|
| 67 |
+
|
| 68 |
+
def __call__(self, prompt: str) -> str:
|
| 69 |
+
"""
|
| 70 |
+
Enables model to be used as a callable by agent frameworks.
|
| 71 |
+
|
| 72 |
+
Args:
|
| 73 |
+
prompt (str): Prompt to send.
|
| 74 |
+
|
| 75 |
+
Returns:
|
| 76 |
+
str: Claude-generated response.
|
| 77 |
+
"""
|
| 78 |
+
return self.complete(prompt)
|
| 79 |
|
| 80 |
# --- Constants ---
|
| 81 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|