Anthropic AI models support
This commit is contained in:
33
lib/gpt_providers/text_generation/anthropic_text_gen.py
Normal file
33
lib/gpt_providers/text_generation/anthropic_text_gen.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import os
|
||||
|
||||
import anthropic
|
||||
from anthropic import Anthropic
|
||||
import streamlit as st
|
||||
|
||||
|
||||
def anthropic_text_response(prompt):
|
||||
""" """
|
||||
client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"),)
|
||||
|
||||
try:
|
||||
response = client.messages.create(
|
||||
max_tokens=1024,
|
||||
messages=[
|
||||
{
|
||||
"role": "user",
|
||||
"content": prompt,
|
||||
}
|
||||
],
|
||||
# This will come from config file.
|
||||
model="claude-3-opus-20240229",
|
||||
)
|
||||
return(message.content)
|
||||
except anthropic.APIConnectionError as e:
|
||||
st.error("The server could not be reached")
|
||||
st.error(e.__cause__) # an underlying Exception, likely raised within httpx.
|
||||
except anthropic.RateLimitError as e:
|
||||
st.error("A 429 status code was received; we should back off a bit.")
|
||||
except anthropic.APIStatusError as e:
|
||||
st.error("Another non-200-range status code was received")
|
||||
st.error(e.status_code)
|
||||
st.error(e.response)
|
||||
@@ -14,6 +14,7 @@ logger.add(sys.stdout,
|
||||
|
||||
from .openai_text_gen import openai_chatgpt
|
||||
from .gemini_pro_text import gemini_text_response
|
||||
from .anthropic_text_gen import anthropic_text_response
|
||||
from ...utils.read_main_config_params import read_return_config_section
|
||||
|
||||
|
||||
@@ -49,6 +50,14 @@ def llm_text_gen(prompt):
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to get response from Openai: {err}")
|
||||
raise err
|
||||
elif 'anthropic' in gpt_provider.lower():
|
||||
try:
|
||||
logger.info(f"Using Anthropic Model: {model} for text Generation.")
|
||||
response = anthropic_text_response(prompt)
|
||||
return response
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to get response from Anthropic: {err}")
|
||||
raise err
|
||||
|
||||
except Exception as err:
|
||||
logger.error(f"Failed to read LLM parameters: {err}")
|
||||
@@ -95,6 +104,8 @@ def get_api_key(gpt_provider):
|
||||
api_key = os.getenv('GEMINI_API_KEY')
|
||||
elif gpt_provider.lower() == 'openai':
|
||||
api_key = os.getenv('OPENAI_API_KEY')
|
||||
elif gpt_provider.lower() == 'anthropic':
|
||||
api_key = os.getenv('ANTHROPIC_API_KEY')
|
||||
|
||||
if not api_key:
|
||||
raise ValueError(f"No API key found for the specified GPT provider: '{gpt_provider}'")
|
||||
|
||||
Reference in New Issue
Block a user