anthropic_model.py 954 B

1234567891011121314151617181920212223242526272829
  1. import anthropic
  2. import json
  3. class AnthropicModel:
  4. def __init__(self, name):
  5. self.name = name
  6. config = json.load(open("config.json"))
  7. self.api_key = config['llms']['anthropic']['api_key'].strip()
  8. self.hparams = config['hparams']
  9. self.hparams.update(config['llms']['anthropic'].get('hparams') or {})
  10. def make_request(self, conversation, add_image=None, logit_bias=None, max_tokens=None):
  11. conversation = [{"role": "user" if i%2 == 0 else "assistant", "content": content} for i,content in enumerate(conversation)]
  12. response = anthropic.Anthropic(api_key=self.api_key).messages.create(
  13. model=self.name,
  14. max_tokens=2048,
  15. messages=conversation
  16. )
  17. return response.content[0].text
  18. if __name__ == "__main__":
  19. import sys
  20. q = "What's your name?"
  21. print(q+":", AnthropicModel("claude-3-5-sonnet-20240620").make_request([q]))