Skip to content

Commit

Permalink
feat: add azure openai support (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
hughcrt authored Apr 16, 2024
1 parent b90b1f0 commit e3ae67f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
26 changes: 23 additions & 3 deletions packages/backend/src/utils/openai.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import OpenAI from "openai"
import { monitorOpenAI } from "lunary/openai"
import OpenAI from "openai"

export function getOpenAIParams() {
if (process.env.OPENAI_API_KEY) {
return {
apiKey: process.env.OPENAI_API_KEY,
}
} else if (process.env.AZURE_OPENAI_API_KEY) {
const apiKey = process.env.AZURE_OPENAI_API_KEY
const model = process.env.AZURE_OPENAI_DEPLOYMENT_ID
const resource = process.env.AZURE_OPENAI_RESOURCE_NAME

const openai = process.env.OPENAI_API_KEY ? monitorOpenAI(new OpenAI()) : null
return {
apiKey,
baseURL: `https://${resource}.openai.azure.com/openai/deployments/${model}`,
defaultQuery: { "api-version": "2024-02-01" },
defaultHeaders: { "api-key": apiKey },
}
} else {
return null
}
}
const clientParams = getOpenAIParams()

export default openai
export default clientParams ? monitorOpenAI(new OpenAI(clientParams)) : null
5 changes: 2 additions & 3 deletions packages/backend/src/utils/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { clearUndefined } from "./ingest"
import OpenAI from "openai"
import { MODELS } from "shared"
import { ReadableStream } from "stream/web"
import { getOpenAIParams } from "./openai"

function convertInputToOpenAIMessages(input: any[]) {
return input.map(({ role, content, text, functionCall, toolCalls, name }) => {
Expand Down Expand Up @@ -228,9 +229,7 @@ export async function runAImodel(
break

case "openai":
clientParams = {
apiKey: process.env.OPENAI_API_KEY,
}
clientParams = getOpenAIParams()
break
case "mistral":
clientParams = {
Expand Down

0 comments on commit e3ae67f

Please sign in to comment.