From 60f0bbaa07fd54aafd05873708fea10639c7d587 Mon Sep 17 00:00:00 2001 From: ajaysi Date: Sat, 29 Jun 2024 10:04:19 +0530 Subject: [PATCH] Anthropic AI models support --- README.md | 7 ++-- .../text_generation/anthropic_text_gen.py | 33 +++++++++++++++++++ .../text_generation/main_text_generation.py | 11 +++++++ requirements.txt | 3 +- 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 lib/gpt_providers/text_generation/anthropic_text_gen.py diff --git a/README.md b/README.md index 5b13e803..8c4d8409 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # How to Alwrity - Getting Started Alwrity assists content creators and digital marketers in keyword web research, AI website & Social media content generation & AI Copywriting. -Our toolkit integrates (OpenAI, Gemini) AI models for text generation, image creation(Stability.ai), STT(whisper, AssemblyAI) and Web or local data analysis, streamlining your content creation pipeline and ensuring high-quality output with minimal effort. +Our toolkit integrates (OpenAI, Gemini, Anthropic) AI models for text generation, image creation(Stability.ai), STT(whisper, AssemblyAI) and Web or local data analysis, streamlining your content creation pipeline and ensuring high-quality output with minimal effort. Prompting is abstracted to get going sooner. Focus on your content quality, rather than AI tooling around it. Alwrity gives personalization, factual web researched & SEO optimized content and tools for automating content & digital marketing. @@ -58,8 +58,11 @@ If you have 💻 Laptop + 🛜 Internet + 10 minutes, you will be generating blo | 14 | Agentic Content Creation | Explores innovative content creation methods with CrewAI. | | 15 | AI Finance Writer | Uses ufinance & padnas_ta to write TA report for given stock symbol | | 16 | AI Agents Team | Easily create AI Agents team for Content creation & Digital marketing | -| 17 | Talk to your Docs | Write content from your local documents of any type(multi-modal) | +| 17 | Talk to your Docs (WIP) | Write content from your local documents of any type(multi-modal) | | 18 | Wordpress API integration | Programmatically upload blogs to wordpress website with API keys | +| 19 | Talk to your website | Crawl Crawl your entire website & write content based on its content, Or Not | +| 20 | Content From URLs | Provide any URL to create an original, unique content from | + --- # AI Content Generation Toolkit - Alwrity diff --git a/lib/gpt_providers/text_generation/anthropic_text_gen.py b/lib/gpt_providers/text_generation/anthropic_text_gen.py new file mode 100644 index 00000000..de432b8c --- /dev/null +++ b/lib/gpt_providers/text_generation/anthropic_text_gen.py @@ -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) diff --git a/lib/gpt_providers/text_generation/main_text_generation.py b/lib/gpt_providers/text_generation/main_text_generation.py index 466f0be2..7395730a 100644 --- a/lib/gpt_providers/text_generation/main_text_generation.py +++ b/lib/gpt_providers/text_generation/main_text_generation.py @@ -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}'") diff --git a/requirements.txt b/requirements.txt index 67848e85..f5c2d471 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,7 +9,7 @@ crewai_tools python-docx PyPDF2 google.generativeai -mistralai +anthropic tenacity tavily-python tabulate @@ -35,3 +35,4 @@ pandas_ta firecrawl-py gTTS streamlit-mic-recorder +