Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Azure OpenAI provider #327

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Binary file modified bun.lockb
100755 → 100644
Binary file not shown.
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
3 changes: 2 additions & 1 deletion 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 All @@ -27,7 +28,7 @@
"@radix-ui/react-tooltip": "^1.0.7",
"@tailwindcss/typography": "^0.5.12",
"@upstash/redis": "^1.30.0",
"ai": "^3.2.22",
"ai": "^3.3.25",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"embla-carousel-react": "^8.0.0",
Expand Down