From 258f0a69857c2b518544181302d09cac6daabc87 Mon Sep 17 00:00:00 2001 From: Christo Olivier Date: Tue, 9 Jul 2024 13:23:32 +0100 Subject: [PATCH] Updated readme with Anthropic details. Update AnthropicModel to deepcopy settings. --- README.md | 6 ++++++ pyproject.toml | 2 +- src/modelsmith/language_models.py | 15 ++++++++++----- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 893deb8..bc21cbe 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/pyproject.toml b/pyproject.toml index 4d044ed..1016ca1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] maintainers = ["Christo Olivier "] diff --git a/src/modelsmith/language_models.py b/src/modelsmith/language_models.py index 64a4b9f..5fbd462 100644 --- a/src/modelsmith/language_models.py +++ b/src/modelsmith/language_models.py @@ -1,3 +1,4 @@ +import copy from abc import ABC, abstractmethod from typing import Any @@ -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) @@ -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