-
-
Save RockAfeller2013/4e501fd9c52e10f3318895556afb832c to your computer and use it in GitHub Desktop.
This Python script demonstrates how to interact with multiple AI models from different providers using their respective APIs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from dotenv import load_dotenv | |
from openai import OpenAI | |
from groq import Groq | |
import anthropic | |
import google.generativeai as genai | |
# Use this pip install command: | |
# python3 -m pip install openai groq anthropic google-generativeai python-dotenv | |
# You will need to create a .env file in the same directory with the following variables: | |
# OPENAI_API_KEY --> https://platform.openai.com/api-keys | |
# GROQ_API_KEY --> https://console.groq.com/keys | |
# ANTHROPIC_API_KEY --> https://console.anthropic.com/api-keys | |
# GOOGLE_API_KEY --> https://aistudio.google.com/app/apikey | |
# | |
# For Example: | |
# | |
#OPENAI_API_KEY=<your-openai-api-key> | |
#GROQ_API_KEY=<your-groq-api-key> | |
#ANTHROPIC_API_KEY=<your-anthropic-api-key> | |
#GOOGLE_API_KEY=<your-google-api-key> | |
load_dotenv() | |
# -----( OPENAI )----------------------------------------------- | |
def generate_openai_response( | |
model="gpt-4o", | |
system_role="You are a helpful assistant", | |
user_prompt="How are you today? Please introduce yourself.", | |
temperature=1, | |
max_tokens=1000, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
): | |
client = OpenAI() | |
response = client.chat.completions.create( | |
model=model, | |
messages=[ | |
{ | |
"role": "system", | |
"content": [ | |
{ | |
"type": "text", | |
"text": system_role | |
} | |
] | |
}, | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "text", | |
"text": user_prompt | |
} | |
] | |
} | |
], | |
temperature=temperature, | |
max_tokens=max_tokens, | |
top_p=top_p, | |
frequency_penalty=frequency_penalty, | |
presence_penalty=presence_penalty | |
) | |
return response.choices[0].message.content | |
# -----( GROQ )----------------------------------------------- | |
def generate_groq_response( | |
model="llama3-70b-8192", | |
system_role="You are a helpful assistant", | |
user_prompt="How are you today? Please introduce yourself.", | |
temperature=0.7, | |
max_tokens=1000 | |
): | |
client = Groq( | |
api_key=os.environ.get("GROQ_API_KEY"), | |
) | |
chat_completion = client.chat.completions.create( | |
messages=[ | |
{"role": "system", "content": system_role}, | |
{"role": "user", "content": user_prompt} | |
], | |
model=model, | |
temperature=temperature, | |
max_tokens=max_tokens, | |
) | |
return chat_completion.choices[0].message.content | |
# -----( ANTHROPIC )----------------------------------------------- | |
def generate_anthropic_response( | |
model="claude-3-opus-20240229", | |
system_role="You are a helpful assistant", | |
user_prompt="How are you today? Please introduce yourself.", | |
temperature=0.7, | |
max_tokens=1000 | |
): | |
client = anthropic.Anthropic( | |
api_key=os.environ.get("ANTHROPIC_API_KEY"), | |
) | |
message = client.messages.create( | |
model=model, | |
max_tokens=max_tokens, | |
temperature=temperature, | |
system=system_role, | |
messages=[ | |
{"role": "user", "content": user_prompt} | |
] | |
) | |
return message.content[0].text | |
# -----( GEMINI )----------------------------------------------- | |
def generate_gemini_response( | |
model="gemini-pro", | |
system_role="You are a helpful assistant", | |
user_prompt="How are you today? Please introduce yourself.", | |
temperature=1, | |
max_tokens=1000 | |
): | |
model = genai.GenerativeModel('gemini-1.5-flash') | |
response = model.generate_content( | |
user_prompt, | |
generation_config=genai.types.GenerationConfig( | |
# Only one candidate for now. | |
candidate_count=1, | |
max_output_tokens=max_tokens, | |
temperature=temperature | |
) | |
) | |
return response.text | |
if __name__ == "__main__": | |
print("\n") | |
print("-" * 80) | |
response = generate_openai_response() | |
print(response) | |
print("-" * 80) | |
response = generate_groq_response() | |
print(response) | |
print("-" * 80) | |
response = generate_anthropic_response() | |
print(response) | |
print("-" * 80) | |
response = generate_gemini_response() | |
print(response) | |
print("-" * 80) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment