Skip to content

Commit 0509f80

Browse files
committed
Merge branch 'tthoraldson-feature/claude' into 0.3.0
2 parents 7963e3c + 1e280c7 commit 0509f80

File tree

4 files changed

+114
-4
lines changed

4 files changed

+114
-4
lines changed

docs/llm_api_wrappers.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,35 @@ raw_llm_output, guardrail_output, *rest = guard(
7171
)
7272
```
7373

74+
## Anthropic
75+
76+
### Completion
77+
78+
```python
79+
from anthropic import Anthropic
80+
import guardrails as gd
81+
82+
# Create a Guard class
83+
guard = gd.Guard.from_rail(...)
84+
85+
# Create an Anthropic client
86+
anthropic_client = Anthropic(api_key="my_api_key")
87+
88+
# Wrap Anthropic API call
89+
raw_llm_output, guardrail_output = guard(
90+
anthropic_client.completions.create,
91+
prompt_params={
92+
"prompt_param_1": "value_1",
93+
"prompt_param_2": "value_2",
94+
...
95+
},
96+
model="claude-2",
97+
max_tokens_to_sample=100,
98+
...
99+
)
100+
```
101+
102+
74103
## Using Manifest
75104
[Manifest](https://github.com/HazyResearch/manifest) is a wrapper around most model APIs and supports hosting local models. It can be used as a LLM API.
76105

guardrails/llm_providers.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,53 @@ def _invoke_llm(
240240
)
241241

242242

243+
class AnthropicCallable(PromptCallableBase):
244+
def _invoke_llm(
245+
self,
246+
prompt: str,
247+
client_callable: Any,
248+
model: str = "claude-instant-1",
249+
max_tokens_to_sample: int = 100,
250+
*args,
251+
**kwargs,
252+
) -> LLMResponse:
253+
"""Wrapper for Anthropic Completions.
254+
255+
To use Anthropic for guardrails, do
256+
```
257+
client = anthropic.Anthropic(api_key=...)
258+
259+
raw_llm_response, validated_response = guard(
260+
client,
261+
model="claude-2",
262+
max_tokens_to_sample=200,
263+
prompt_params={...},
264+
...
265+
```
266+
"""
267+
try:
268+
import anthropic
269+
except ImportError:
270+
raise PromptCallableException(
271+
"The `anthropic` package is not installed. "
272+
"Install with `pip install anthropic`"
273+
)
274+
275+
if "instructions" in kwargs:
276+
prompt = kwargs.pop("instructions") + "\n\n" + prompt
277+
278+
anthropic_prompt = f"{anthropic.HUMAN_PROMPT} {prompt} {anthropic.AI_PROMPT}"
279+
280+
anthropic_response = client_callable(
281+
model=model,
282+
prompt=anthropic_prompt,
283+
max_tokens_to_sample=max_tokens_to_sample,
284+
*args,
285+
**kwargs,
286+
)
287+
return LLMResponse(output=anthropic_response.completion)
288+
289+
243290
class ArbitraryCallable(PromptCallableBase):
244291
def __init__(self, llm_api: Callable, *args, **kwargs):
245292
self.llm_api = llm_api
@@ -314,6 +361,17 @@ def get_llm_ask(llm_api: Callable, *args, **kwargs) -> PromptCallableBase:
314361
except ImportError:
315362
pass
316363

364+
try:
365+
import anthropic.resources # noqa: F401 # type: ignore
366+
367+
if isinstance(
368+
getattr(llm_api, "__self__", None),
369+
anthropic.resources.completions.Completions,
370+
):
371+
return AnthropicCallable(*args, client_callable=llm_api, **kwargs)
372+
except ImportError:
373+
pass
374+
317375
# Let the user pass in an arbitrary callable.
318376
return ArbitraryCallable(*args, llm_api=llm_api, **kwargs)
319377

poetry.lock

Lines changed: 25 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ torch = {version = "^2.1.0", optional = true, source = "torch"}
4545
presidio_analyzer = {version = "^2.2.33", optional = true}
4646
presidio_anonymizer = {version = "^2.2.33", optional = true}
4747
spacy = {version = "^3.7.2", optional = true}
48+
anthropic = {version = "^0.7.2", optional = true}
4849

4950
[tool.poetry.extras]
5051
sql = ["sqlvalidator", "sqlalchemy", "sqlglot"]
@@ -57,6 +58,7 @@ critique = ["inspiredco"]
5758
toxic_language = ["transformers", "torch"]
5859
pii = ["presidio_analyzer", "presidio_anonymizer"]
5960
competitor-check = ["spacy"]
61+
anthropic = ["anthropic"]
6062

6163
[tool.poetry.group.dev.dependencies]
6264
pytest = "^7.4.3"

0 commit comments

Comments
 (0)