In this method, Aporia acts as a proxy, forwarding your requests to OpenAI and simultaneously invoking guardrails. The returned response is either the original from OpenAI or a modified version enforced by Aporia’s policies.This integration supports real-time applications through streaming capabilities, making it particularly useful for chatbots.
If you’re just getting started and your app is based on OpenAI or Azure OpenAI, this method is highly recommended.All you need to do is replace the OpenAI Base URL and add Aporia’s API Key header.
Here is a basic example of how to configure the OpenAI client to use Aporia’s OpenAI Proxy method:
from openai import OpenAIclient = OpenAI( api_key='<your OpenAI API key>', base_url='<the copied base URL>', default_headers={'X-APORIA-API-KEY': '<your Aporia API key>'})chat_completion = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ { "role": "user", "content": "Hello world", } ], user="<end-user ID>",)
import OpenAI from "openai";const openai = new OpenAI({ apiKey: "<your OpenAI API key>", baseURL: "<the copied URL>", defaultHeaders: {"X-APORIA-API-KEY": "<your Aporia API key>"},});async function chat() { const completion = await openai.chat.completions.create({ messages: [{ role: "system", content: "You are a helpful assistant." }], model: "gpt-3.5-turbo", user: "<end-user ID>", });}
import { ChatOpenAI } from "@langchain/openai";const model = new ChatOpenAI({ apiKey: "<your OpenAI API key>", configuration: { baseURL: "<the copied URL>", defaultHeaders: {"X-APORIA-API-KEY": "<your Aporia API key>"}, }, user: "<end-user ID>",});const response = await model.invoke( "What would be a good company name a company that makes colorful socks?");console.log(response);