Gemini
Access Gemini models through LangDB’s API using OpenAI SDK
Was this helpful?
Access Gemini models through LangDB’s API using OpenAI SDK
Was this helpful?
LangDB provides first class support for Gemini models.
You can use to run the Gemini models.
# Please install OpenAI SDK first: `pip3 install openai`
from openai import OpenAI
api_base = "https://api.us-east-1.langdb.ai" # LangDB API base URL
api_key = "xxxxx" # Replace with your LangDB token
default_headers = {"x-project-id": "xxxxx"} # LangDB Project ID
client = OpenAI(
base_url=api_base,
api_key=api_key,
)
response = client.chat.completions.create(
model="gemini/gemini-1.5-flash-8b",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Hello"}
],
stream=False
)
print(response.choices[0].message.content)
// Please install OpenAI SDK first: `npm install openai`
import OpenAI from "openai";
const apiBase = "https://api.us-east-1.langdb.ai"; // LangDB API base URL
const apiKey = "LANGDB_API_KEY"; // Replace with your LangDB token
const defaultHeaders = { "x-project-id": "xxxx" }; // LangDB Project ID
const client = new OpenAI({
baseURL: apiBase,
apiKey: apiKey,
});
async function main() {
const completion = await client.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }, {"role": "user", "content": "Hello"}],
model: "gemini/gemini-1.5-flash-8b",
}, { headers: defaultHeaders });
console.log(completion.choices[0].message.content);
}
main();
curl "https://api.us-east-1.langdb.ai/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LANGDB_API_KEY" \
-X "X-Project-Id: $Project_ID" \
-d '{
"model": "gemini/gemini-1.5-flash-8b",
"messages": [
{
"role": "system",
"content": "You are helpful assistant",
},
{
"role": "user",
"content": "Write a haiku about recursion in programming."
}
]
}'