| 8 | |
| 9 | |
| 10 | class AnthropicDecoder(DecoderBase): |
| 11 | def __init__(self, name: str, **kwargs) -> None: |
| 12 | super().__init__(name, **kwargs) |
| 13 | self.client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_KEY")) |
| 14 | |
| 15 | def codegen( |
| 16 | self, prompt: str, do_sample: bool = True, num_samples: int = 200 |
| 17 | ) -> List[str]: |
| 18 | if do_sample: |
| 19 | assert self.temperature > 0, "Temperature must be positive for sampling" |
| 20 | |
| 21 | batch_size = min(self.batch_size, num_samples) |
| 22 | if not do_sample: |
| 23 | assert batch_size == 1, "Sampling only supports batch size of 1" |
| 24 | |
| 25 | outputs = [] |
| 26 | for _ in range(batch_size): |
| 27 | message = anthropic_request.make_auto_request( |
| 28 | client=self.client, |
| 29 | model=self.name, |
| 30 | messages=[ |
| 31 | { |
| 32 | "role": "user", |
| 33 | "content": self.instruction_prefix |
| 34 | + f"\n```python\n{prompt.strip()}\n```\n", |
| 35 | } |
| 36 | ], |
| 37 | max_tokens=self.max_new_tokens, |
| 38 | temperature=self.temperature, |
| 39 | stop_sequences=self.eos, |
| 40 | ) |
| 41 | outputs.append(message.content[0].text) |
| 42 | |
| 43 | return outputs |
| 44 | |
| 45 | def is_direct_completion(self) -> bool: |
| 46 | return False |
no outgoing calls
no test coverage detected
searching dependent graphs…