OpenAI integration
If you already have a project that uses OpenAI, integrating Not Diamond is only a few line changes.
Installation
Requires Python 3.10+
pip install notdiamond[openai]
npm install notdiamond dotenv
Updating your existing code
- from openai import OpenAI
+ from notdiamond.toolkit.openai import OpenAI
- client = OpenAI(api_key="OPENAI_API_KEY")
+ client = OpenAI()
chat_completion = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a world class programmer."},
{"role": "user", "content": "Write a merge sort in Python"},
],
- model="gpt-4",
+ model=["openai/gpt-4o", "openai/gpt-4o-mini", "openai/gpt-4-turbo-2024-04-09"]
)
print(chat_completion.choices[0].message.content)
- import OpenAI from "openai";
+ import { NotDiamond } from 'notdiamond';
+ require('dotenv').config()
- const openai = new OpenAI();
+ const notDiamond = new NotDiamond({apiKey: process.env.NOTDIAMOND_API_KEY});
async function main() {
- const completion = await openai.chat.completions.create({
+ const result = await notDiamond.modelSelect({
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Write a merge sort in Python" }
],
- model: "gpt-3.5-turbo",
+ llmProviders: [
+ { provider: 'openai', model: 'gpt-4o' },
+ { provider: 'openai', model: 'gpt-3.5-turbo' },
+ { provider: 'anthropic', model: 'claude-3-5-sonnet-20240620' },
+ ],
});
- console.log(completion.choices[0].message.content);
+ console.log(result.content);
}
main();
API keys
You can also set API keys in a
.env
file.
Updated 2 months ago