Get started in minutes
No need for code changes, simply swap your end-point to CogCache and start your acceleration and savings journey.
Quickstart Guide >Quickstart Guide >
Code copied to clipboard!
from openai import OpenAI
COGCACHE_LLM_MODEL = "" # the model of choice
COGCACHE_API_KEY = "" # the generated CogCache API key
client = OpenAI(
base_url = "https://proxy-api.cogcache.com/v1/",
api_key = COGCACHE_API_KEY, # this is not needed here, if it's already set via environment variables
default_headers = {
"Authorization": f"Bearer {COGCACHE_API_KEY}",
},
)
response = client.chat.completions.create(
model = COGCACHE_LLM_MODEL,
stream = True,
messages = [
{
"role": "system",
"content": "Assistant is a large language model trained by OpenAI.",
},
{
"role": "user",
"content": "Write a blog post about Generative AI"
},
],
)
for chunk in response:
print(chunk)
Code copied to clipboard!
from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage
COGCACHE_LLM_MODEL = "" # the model of choice
COGCACHE_API_KEY = "" # the generated CogCache API key
model = ChatOpenAI(
base_url = "https://proxy-api.cogcache.com/v1/",
model = COGCACHE_LLM_MODEL,
openai_api_key = COGCACHE_API_KEY,
default_headers = {
"Authorization": f"Bearer {COGCACHE_API_KEY}"
},
)
response = model.stream(
[
SystemMessage(content="Assistant is a large language model trained by OpenAI."),
HumanMessage(content="Write a blog post about Generative AI"),
],
)
for chunk in response:
print(chunk.content)
Code copied to clipboard!
curl --location 'https://proxy-api.cogcache.com/v1/chat/completions' \
--header 'Authorization: Bearer {COGCACHE_API_KEY}' \
--header 'Content-Type: application/json' \
--data '{
"messages": [
{
"role": "system",
"content": "Assistant is a large language model trained by OpenAI."
},
{
"role": "user",
"content": "Write a blog post about Generative AI"
}
],
"model": "COGCACHE_LLM_MODEL”,
"stream": true
}'
Code copied to clipboard!
import OpenAI from "openai";
const COGCACHE_LLM_MODEL = ""; // the model of choice
const COGCACHE_API_KEY = ""; // the generated CogCache API key
const openai = new OpenAI({
baseURL: "https://proxy-api.cogcache.com/v1/",
apiKey: COGCACHE_API_KEY,
defaultHeaders: {
Authorization: `Bearer ${COGCACHE_API_KEY}`,
},
});
async function main() {
try {
const response = await openai.chat.completions.create({
messages: [
{
role: "system",
content: "Assistant is a large language model trained by OpenAI.",
},
{
role: "user",
content: "Write a blog post about Generative AI"
},
],
model: COGCACHE_LLM_MODEL,
stream: true,
});
// Check if response is async iterable
if (response[Symbol.asyncIterator]) {
for await (const chunk of response) {
const text = chunk.choices[0]?.delta?.content; // Adjust based on actual response structure
if (text) {
console.log(text);
}
}
} else {
console.log('Response is not an async iterable');
}
} catch (error) {
console.error('An error occurred:', error);
}
}
main();
Code copied to clipboard!
import { ChatOpenAI } from "@langchain/openai";
const COGCACHE_LLM_MODEL = ""; // the model of choice
const COGCACHE_API_KEY = ""; // the generated CogCache API key
const model = new ChatOpenAI({
configuration: {
baseURL: "https://proxy-api.cogcache.com/v1/",
baseOptions: {
headers: {
"Authorization": `Bearer ${COGCACHE_API_KEY}`,
},
},
},
apiKey: COGCACHE_API_KEY,
model: COGCACHE_LLM_MODEL,
streaming: true,
});
(async () => {
const response = await model.invoke(
[
["system", "Assistant is a large language model trained by OpenAI."],
["user", "Write a blog post about Generative AI"] ,
],
{
callbacks: [
{
handleLLMNewToken: async (token) => {
console.log("handleLLMNewToken", token);
},
handleLLMEnd: async (output) => {
console.log("handleLLMEnd", output);
},
handleChatModelStart: (...chat) => {
console.log("handleChatModelStart", chat);
},
},
],
}
);
})().catch((err) => {
console.error("Something went wrong:", err);
});