Skip to content

Commit

Permalink
Merge pull request #327 from SzilvasiPeter/main
Browse files Browse the repository at this point in the history
Support Azure OpenAI provider
  • Loading branch information
miurla committed Sep 3, 2024
2 parents eff9ca8 + 2fdccd5 commit 8d357ac
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ SEARXNG_SAFESEARCH=0 # Safe search setting: 0 (off), 1 (moderate), 2 (strict)
# If not set, the default is gpt-4o.
# OPENAI_API_MODEL=gpt-4o-mini

# Azure OpenAI API key retrieved here: https://oai.azure.com/resource/deployments/
# AZURE_API_KEY=

# The resource name is used in the assembled URL: https://{resourceName}.openai.azure.com/openai/deployments/{modelId}{path}.
# AZURE_RESOURCE_NAME=

# Used to set the deployment name for Azure OpenAI API requests.
# If not set, the default is gpt-4o.
# AZURE_DEPLOYMENT_NAME=

# If you want to use Google Generative AI instead of OpenAI, enable the following settings.
# Google Generative AI API key retrieved here: https://aistudio.google.com/app/apikey
# GOOGLE_GENERATIVE_AI_API_KEY=[YOUR_GOOGLE_GENERATIVE_AI_API_KEY]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ An AI-powered search engine with a generative UI.
- Use as a search engine [](#-search-engine)
- Support for providers other than OpenAI
- Google Generative AI Provider
- Azure OpenAI Provider [](https://github.com/miurla/morphic/issues/13)
- Anthropic Provider [](https://github.com/miurla/morphic/pull/239)
- Ollama Provider ([Unstable](https://github.com/miurla/morphic/issues/215))
- Specify the model to generate answers
Expand Down
Empty file modified bun.lockb
100755 → 100644
Empty file.
16 changes: 14 additions & 2 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
import { createOllama } from 'ollama-ai-provider'
import { createOpenAI } from '@ai-sdk/openai'
import { createAzure } from '@ai-sdk/azure';
import { google } from '@ai-sdk/google'
import { anthropic } from '@ai-sdk/anthropic'
import { CoreMessage } from 'ai'
Expand All @@ -17,6 +18,9 @@ export function getModel(useSubModel = false) {
const openaiApiBase = process.env.OPENAI_API_BASE
const openaiApiKey = process.env.OPENAI_API_KEY
let openaiApiModel = process.env.OPENAI_API_MODEL || 'gpt-4o'
const azureResourceName = process.env.AZURE_RESOURCE_NAME
const azureApiKey = process.env.AZURE_API_KEY
let azureDeploymentName = process.env.AZURE_DEPLOYMENT_NAME || 'gpt-4o'
const googleApiKey = process.env.GOOGLE_GENERATIVE_AI_API_KEY
const anthropicApiKey = process.env.ANTHROPIC_API_KEY

Expand All @@ -27,7 +31,7 @@ export function getModel(useSubModel = false) {
!anthropicApiKey
) {
throw new Error(
'Missing environment variables for Ollama, OpenAI, Google or Anthropic'
'Missing environment variables for Ollama, OpenAI, Azure OpenAI, Google or Anthropic'
)
}
// Ollama
Expand All @@ -49,8 +53,16 @@ export function getModel(useSubModel = false) {
return anthropic('claude-3-5-sonnet-20240620')
}

// Fallback to OpenAI instead
if (azureApiKey && azureResourceName) {
const azure = createAzure({
apiKey: azureApiKey,
resourceName: azureResourceName
})

return azure.chat(azureDeploymentName)
}

// Fallback to OpenAI instead
const openai = createOpenAI({
baseURL: openaiApiBase, // optional base URL for proxies etc.
apiKey: openaiApiKey, // optional API key, default to env property OPENAI_API_KEY
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@ai-sdk/anthropic": "^0.0.21",
"@ai-sdk/azure": "^0.0.32",
"@ai-sdk/google": "^0.0.22",
"@ai-sdk/openai": "^0.0.31",
"@radix-ui/react-alert-dialog": "^1.0.5",
Expand Down

0 comments on commit 8d357ac

Please sign in to comment.