Spaces:
Paused
Paused
matt HOFFNER
commited on
Commit
·
888d022
1
Parent(s):
5b2069d
fix
Browse files- src/pages/api/stream.js +5 -12
src/pages/api/stream.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
-
import { AZURE_DEPLOYMENT_ID, OPENAI_API_HOST, OPENAI_API_TYPE, OPENAI_API_VERSION, OPENAI_ORGANIZATION } from './llm';
|
| 2 |
import { createParser } from 'eventsource-parser';
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
export class LLMError extends Error {
|
| 5 |
constructor(message, type, param, code) {
|
| 6 |
super(message);
|
|
@@ -19,21 +21,12 @@ export const LLMStream = async (
|
|
| 19 |
messages
|
| 20 |
) => {
|
| 21 |
let url = `${OPENAI_API_HOST}/v1/chat/completions`;
|
| 22 |
-
if (OPENAI_API_TYPE === 'azure') {
|
| 23 |
-
url = `${OPENAI_API_HOST}/openai/deployments/${AZURE_DEPLOYMENT_ID}/chat/completions?api-version=${OPENAI_API_VERSION}`;
|
| 24 |
-
}
|
| 25 |
const res = await fetch(url, {
|
| 26 |
headers: {
|
| 27 |
'Content-Type': 'application/json',
|
| 28 |
...(OPENAI_API_TYPE === 'openai' && {
|
| 29 |
Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`
|
| 30 |
-
})
|
| 31 |
-
...(OPENAI_API_TYPE === 'azure' && {
|
| 32 |
-
'api-key': `${key ? key : process.env.OPENAI_API_KEY}`
|
| 33 |
-
}),
|
| 34 |
-
...((OPENAI_API_TYPE === 'openai' && OPENAI_ORGANIZATION) && {
|
| 35 |
-
'OpenAI-Organization': OPENAI_ORGANIZATION,
|
| 36 |
-
}),
|
| 37 |
},
|
| 38 |
method: 'POST',
|
| 39 |
body: JSON.stringify({
|
|
@@ -57,7 +50,7 @@ export const LLMStream = async (
|
|
| 57 |
if (res.status !== 200) {
|
| 58 |
const result = await res.json();
|
| 59 |
if (result.error) {
|
| 60 |
-
throw new
|
| 61 |
result.error.message,
|
| 62 |
result.error.type,
|
| 63 |
result.error.param,
|
|
|
|
|
|
|
| 1 |
import { createParser } from 'eventsource-parser';
|
| 2 |
|
| 3 |
+
export const OPENAI_API_HOST = process.env.OPENAI_API_HOST || "https://api.openai.com";
|
| 4 |
+
export const OPENAI_API_TYPE = process.env.OPENAI_API_TYPE || "openai";
|
| 5 |
+
|
| 6 |
export class LLMError extends Error {
|
| 7 |
constructor(message, type, param, code) {
|
| 8 |
super(message);
|
|
|
|
| 21 |
messages
|
| 22 |
) => {
|
| 23 |
let url = `${OPENAI_API_HOST}/v1/chat/completions`;
|
|
|
|
|
|
|
|
|
|
| 24 |
const res = await fetch(url, {
|
| 25 |
headers: {
|
| 26 |
'Content-Type': 'application/json',
|
| 27 |
...(OPENAI_API_TYPE === 'openai' && {
|
| 28 |
Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`
|
| 29 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
},
|
| 31 |
method: 'POST',
|
| 32 |
body: JSON.stringify({
|
|
|
|
| 50 |
if (res.status !== 200) {
|
| 51 |
const result = await res.json();
|
| 52 |
if (result.error) {
|
| 53 |
+
throw new LLMError(
|
| 54 |
result.error.message,
|
| 55 |
result.error.type,
|
| 56 |
result.error.param,
|