Setting the API key for Cohere and Anthropic per instance #15735
-
I have a use-case where I would like to change the API key for either a Cohere LLM or an Anthropic LLM on a per instance basis. However, I do not want to create new instances of the LLM objects. With LLM objects such as Groq, Open AI and others, it is possible to call AttributeError: 'Cohere' object has no attribute 'api_key' Why is that even though I can set For reference, I am using the following imports. from llama_index.llms.groq import Groq
from llama_index.llms.anthropic import Anthropic
from llama_index.llms.cohere import Cohere
from llama_index.llms.openai import OpenAI |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The inability to set the For the If you need to change the |
Beta Was this translation helpful? Give feedback.
-
For Anthropic models, I believe this works: from llama_index.llms.anthropic import Anthropic
my_api_key = 'sk-***'
llm_model = Anthropic()
llm_model._client.api_key = my_api_key
llm_model._aclient.api_key = my_api_key |
Beta Was this translation helpful? Give feedback.
The inability to set the
api_key
attribute on aCohere
LLM object after instantiation is intended behavior, not a bug. This is because theapi_key
is used during the initialization of the_client
and_aclient
attributes, which are instances ofcohere.Client
andcohere.AsyncClient
, respectively. These clients are created in the__init__
method and require theapi_key
at that time. Changing theapi_key
after instantiation would not update these client instances [1].For the
Anthropic
class, I wasn't able to find specific information within the repository, but it is likely that it follows a similar pattern where theapi_key
is used during initialization and cannot be changed afterward.If y…