Datawithsarah commited on
Commit
9e54048
·
1 Parent(s): 3707bff

updated ClaudeServerModel with stop sequence

Browse files
Files changed (1) hide show
  1. app.py +7 -33
app.py CHANGED
@@ -25,28 +25,11 @@ class ClaudeServerModel:
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",
@@ -58,24 +41,15 @@ class ClaudeServerModel:
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"
 
25
  """
26
 
27
  def __init__(self, api_key: str, model_id: str = "claude-3-opus-20240229", temperature: float = 0.0):
 
 
 
 
 
 
 
 
28
  self.api_key = api_key
29
  self.model_id = model_id
30
  self.temperature = temperature
31
 
32
+ def complete(self, prompt: str, stop_sequences: list[str] = None) -> str:
 
 
 
 
 
 
 
 
 
33
  headers = {
34
  "x-api-key": self.api_key,
35
  "anthropic-version": "2023-06-01",
 
41
  "temperature": self.temperature,
42
  "messages": [{"role": "user", "content": prompt}]
43
  }
44
+ if stop_sequences:
45
+ body["stop_sequences"] = stop_sequences
46
+
47
  response = requests.post("https://api.anthropic.com/v1/messages", headers=headers, json=body)
48
  response.raise_for_status()
49
+ return response.json()["content"][0]["text"].strip()
 
 
 
 
 
 
 
 
 
 
50
 
51
+ def __call__(self, prompt: str, stop_sequences: list[str] = None) -> str:
52
+ return self.complete(prompt, stop_sequences=stop_sequences)
 
 
53
 
54
  # --- Constants ---
55
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"