Skip to content

Commit

Permalink
Use AzureOpenAI If both Azure and OpenAI are available (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
blrchen authored Mar 31, 2023
1 parent 127fab7 commit 753defd
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
58 changes: 58 additions & 0 deletions app/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions app/.idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 11 additions & 9 deletions app/src/pages/api/chat-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ const handler = async (req: Request): Promise<Response> => {
messagesToSend.push(message)
}

const useOpenAI = process.env.OPENAI_API_KEY && process.env.OPENAI_API_KEY.length > 0
const useAzureOpenAI =
process.env.AZURE_OPENAI_BASE_URL && process.env.AZURE_OPENAI_BASE_URL.length > 0

let apiUrl: string
let apiKey: string
let model: string
if (useOpenAI) {
const apiBaseUrl = process.env.OPENAI_BASE_URL || 'https://api.openai.com'
apiUrl = `${apiBaseUrl}/v1/chat/completions`
apiKey = process.env.OPENAI_API_KEY || ''
model = 'gpt-3.5-turbo'
} else {
if (useAzureOpenAI) {
const apiBaseUrl = process.env.AZURE_OPENAI_BASE_URL
const version = '2023-03-15-preview'
const deployment = process.env.AZURE_OPENAI_DEPLOYMENT || ''
apiUrl = `${apiBaseUrl}/openai/deployments/${deployment}/chat/completions?api-version=${version}`
apiKey = process.env.AZURE_OPENAI_API_KEY || ''
model = 'gpt-35-turbo'
} else {
const apiBaseUrl = process.env.OPENAI_BASE_URL || 'https://api.openai.com'
apiUrl = `${apiBaseUrl}/v1/chat/completions`
apiKey = process.env.OPENAI_API_KEY || ''
model = 'gpt-3.5-turbo'
}

const stream = await OpenAIStream(apiUrl, apiKey, model, messagesToSend)

return new Response(stream)
Expand Down Expand Up @@ -83,7 +83,9 @@ const OpenAIStream = async (apiUrl: string, apiKey: string, model: string, messa

if (res.status !== 200) {
const statusText = res.statusText
throw new Error(`OpenAI API returned an error: ${statusText}`)
throw new Error(
`The OpenAI API has encountered an error with a status code of ${res.status} and message ${statusText}`
)
}

return new ReadableStream({
Expand Down
2 changes: 1 addition & 1 deletion app/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function Home() {
<HeaderBar />
<Content className={styles.main}>
{/*<ChatGPT fetchPath="http://localhost:3000/api/chat-completion" />*/}
<ChatGPT fetchPath="https://gptlite-afi11hliq-blrchen.vercel.app/api/chat-completion" />
<ChatGPT fetchPath="https://gptlite.vercel.app/api/chat-completion" />
</Content>
<FooterBar />
</Layout>
Expand Down

0 comments on commit 753defd

Please sign in to comment.