Prerequisites
First, install the required packages:
pip install openai anthropic google-generativeai requests python-dotenv
Import Required Libraries
# imports
import os
from dotenv import load_dotenv
from openai import OpenAI
import google.generativeai
import anthropic
from IPython.display import Markdown, display, update_display
# Load environment variables in a file called .env
load_dotenv()
os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')
os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')
os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your-key-if-not-using-env')
Connect to OpenAI, Anthropic and Google
All 3 APIs are similar
Having problems with API files? You can use
openai = OpenAI(api_key=”your-key-here”) and same for claude
openai = OpenAI()
claude = anthropic.Anthropic()
google.generativeai.configure()
What information is included in the API
Typically we’ll pass to the API:
The name of the model that should be used.
Learn about Medium’s values
System Message:
The system message sets the context for the assistant. This message informs the model about its role, tone, and any specific guidelines it should follow while responding.
User Message:
The user message represents the input from the user, such as questions or instructions, which the model responds to. It contains the primary prompt or query you want the model to answer.
There are other parameters that can be used, including temperature which is typically between 0 and 1; higher for more random output; lower for more focused and deterministic.