Datawithsarah commited on
Commit
09b53af
·
1 Parent(s): e949b7f

Claude API fix

Browse files
Files changed (1) hide show
  1. app.py +8 -5
app.py CHANGED
@@ -20,8 +20,7 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
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):
@@ -29,7 +28,7 @@ class ClaudeServerModel:
29
  self.model_id = model_id
30
  self.temperature = temperature
31
 
32
- def complete(self, prompt: str) -> str:
33
  headers = {
34
  "x-api-key": self.api_key,
35
  "anthropic-version": "2023-06-01",
@@ -43,12 +42,16 @@ class ClaudeServerModel:
43
  "prompt": f"\n\nHuman: {prompt}\n\nAssistant:"
44
  }
45
 
 
 
 
 
46
  response = requests.post("https://api.anthropic.com/v1/complete", headers=headers, json=body)
47
  response.raise_for_status()
48
  return response.json()["completion"].strip()
49
 
50
- def __call__(self, prompt: str) -> str:
51
- return self.complete(prompt)
52
 
53
  # --- Constants ---
54
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
20
  # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
21
  class ClaudeServerModel:
22
  """
23
+ ClaudeServerModel wraps Anthropic Claude API for smolagents-style usage.
 
24
  """
25
 
26
  def __init__(self, api_key: str, model_id: str = "claude-3-opus-20240229", temperature: float = 0.0):
 
28
  self.model_id = model_id
29
  self.temperature = temperature
30
 
31
+ def complete(self, prompt: str, stop_sequences: list[str] = None) -> str:
32
  headers = {
33
  "x-api-key": self.api_key,
34
  "anthropic-version": "2023-06-01",
 
42
  "prompt": f"\n\nHuman: {prompt}\n\nAssistant:"
43
  }
44
 
45
+ # Claude expects stop_sequences as "stop_sequences", if passed
46
+ if stop_sequences:
47
+ body["stop_sequences"] = stop_sequences
48
+
49
  response = requests.post("https://api.anthropic.com/v1/complete", headers=headers, json=body)
50
  response.raise_for_status()
51
  return response.json()["completion"].strip()
52
 
53
+ def __call__(self, prompt: str, stop_sequences: list[str] = None) -> str:
54
+ return self.complete(prompt, stop_sequences=stop_sequences)
55
 
56
  # --- Constants ---
57
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"