Skip to content

Commit

Permalink
Updated readme with Anthropic details. Update AnthropicModel to deepc…
Browse files Browse the repository at this point in the history
…opy settings.
  • Loading branch information
christo-olivier committed Jul 9, 2024
1 parent a236657 commit 258f0a6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Install Modelsmith using pip or your favourite python package manager.
pip install modelsmith
```

## Anthropic Authentication

Authentication to Anthropic is done via the Anthropic flow. See the [Anthropic documentation](https://docs.anthropic.com/en/docs/quickstart#set-your-api-key) for more details.

The `AnthropicModel` class takes an optional `api_key` parameter. If not provided, the `ANTHROPIC_API_KEY` environment variable will be used.

## Google Cloud Authentication

Authentication to Google Cloud is done via the Application Default Credentials flow. So make sure you have ADC configured. See [Google's documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc) for more details.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "modelsmith"
version = "0.6.0"
version = "0.6.1"
description = "Get Pydantic models and Python types as LLM responses from Google Vertex AI and OpenAI models."
authors = ["Christo Olivier <[email protected]>"]
maintainers = ["Christo Olivier <[email protected]>"]
Expand Down
15 changes: 10 additions & 5 deletions src/modelsmith/language_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
from abc import ABC, abstractmethod
from typing import Any

Expand Down Expand Up @@ -34,6 +35,10 @@ class AnthropicModel(BaseLanguageModel):
def __init__(self, model_name: str, api_key: str | None = None) -> None:
"""
Create a new synchronous anthropic client instance.
This automatically infers the following arguments from their corresponding
environment variables if they are not provided:
- `api_key` from ANTHROPIC_API_KEY
"""
self.model_name = model_name
self._client = Anthropic(api_key=api_key)
Expand All @@ -48,15 +53,15 @@ def send(self, input: str, model_settings: dict[str, Any] | None = None) -> str:
:return: The response from the LLM.
"""
# If `max_tokens` not provided in model settings then set it to the default
# of 1024
model_settings = model_settings or {}
if "max_tokens" not in model_settings:
model_settings["max_tokens"] = DEFAULT_MAX_TOKENS
# of 1024. Do a deep copy so that the original model settings are not modified.
settings = copy.deepcopy(model_settings) if model_settings else {}
if "max_tokens" not in settings:
settings["max_tokens"] = DEFAULT_MAX_TOKENS

response = self._client.messages.create(
model=self.model_name,
messages=[{"role": "user", "content": input}],
**(model_settings or {}),
**settings,
)

return response.content[0].text
Expand Down

0 comments on commit 258f0a6

Please sign in to comment.