Spaces:
Configuration error
Configuration error
Download clients/python/anthropic.py from AnthropicallyHuggingFaces/ai-platform-toolbox: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/AnthropicallyHuggingFaces/ai-platform-toolbox/resolve/main/clients/python/anthropic.py
- Command line
-
hf download hf://spaces/AnthropicallyHuggingFaces/ai-platform-toolbox/clients/python/anthropic.py
-
curl -L -o anthropic.py https://huggingface.co/spaces/AnthropicallyHuggingFaces/ai-platform-toolbox/resolve/main/clients/python/anthropic.py
1.06 kB
| """Anthropic Claude client - messages, tool use, prompt caching.""" | |
| import os | |
| import requests | |
| BASE = os.getenv("ANTHROPIC_API_BASE", "https://api.anthropic.com/v1") | |
| KEY = os.getenv("ANTHROPIC_API_KEY") | |
| def _h(): | |
| if not KEY: | |
| raise RuntimeError("ANTHROPIC_API_KEY not set") | |
| return {"x-api-key": KEY, "anthropic-version": "2023-06-01"} | |
| def messages(prompt, model="claude-opus", system=None, tools=None, max_tokens=4096, **kwargs): | |
| body = {"model": model, "max_tokens": max_tokens, "messages": [{"role": "user", "content": prompt}]} | |
| if system: | |
| body["system"] = system | |
| if tools: | |
| body["tools"] = tools | |
| body.update(kwargs) | |
| return requests.post(f"{BASE}/messages", headers=_h(), json=body, timeout=120).json() | |
| def tool_result_reply(tool_results, model="claude-opus", max_tokens=4096): | |
| """Continue a tool-use loop with tool results.""" | |
| body = {"model": model, "max_tokens": max_tokens, "messages": tool_results} | |
| return requests.post(f"{BASE}/messages", headers=_h(), json=body, timeout=120).json() | |