Spaces:
Sleeping
Sleeping
Julian Bilcke
commited on
Commit
·
6463491
1
Parent(s):
9074714
the dynamic import is causing issue during build, but it's not an issue we can just put the var init inside the function
Browse files
src/app/queries/predict.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
| 1 |
"use server"
|
| 2 |
|
| 3 |
import { LLMEngine } from "@/types"
|
|
|
|
|
|
|
| 4 |
|
| 5 |
const llmEngine = `${process.env.LLM_ENGINE || ""}` as LLMEngine
|
| 6 |
|
| 7 |
-
export const predict =
|
| 8 |
-
if (llmEngine === "OPENAI") {
|
| 9 |
-
return (await import("./predictWithOpenAI")).predictWithOpenAI
|
| 10 |
-
} else {
|
| 11 |
-
return (await import("./predictWithHuggingFace")).predictWithHuggingFace
|
| 12 |
-
}
|
| 13 |
-
}
|
|
|
|
| 1 |
"use server"
|
| 2 |
|
| 3 |
import { LLMEngine } from "@/types"
|
| 4 |
+
import { predict as predictWithHuggingFace } from "./predictWithHuggingFace"
|
| 5 |
+
import { predict as predictWithOpenAI } from "./predictWithOpenAI"
|
| 6 |
|
| 7 |
const llmEngine = `${process.env.LLM_ENGINE || ""}` as LLMEngine
|
| 8 |
|
| 9 |
+
export const predict = llmEngine === "OPENAI" ? predictWithOpenAI : predictWithHuggingFace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/app/queries/predictWithHuggingFace.ts
CHANGED
|
@@ -3,45 +3,45 @@
|
|
| 3 |
import { HfInference, HfInferenceEndpoint } from "@huggingface/inference"
|
| 4 |
import { LLMEngine } from "@/types"
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
const llmEngine = `${process.env.LLM_ENGINE || ""}` as LLMEngine
|
| 9 |
-
const inferenceEndpoint = `${process.env.LLM_HF_INFERENCE_ENDPOINT_URL || ""}`
|
| 10 |
-
const inferenceModel = `${process.env.LLM_HF_INFERENCE_API_MODEL || ""}`
|
| 11 |
|
| 12 |
-
let hfie: HfInferenceEndpoint = hf
|
| 13 |
|
| 14 |
-
switch (llmEngine) {
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
console.error(error)
|
| 32 |
throw new Error(error)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
default:
|
| 37 |
-
const error = "Please check your Hugging Face Inference API or Inference Endpoint settings"
|
| 38 |
-
console.error(error)
|
| 39 |
-
throw new Error(error)
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
const api = llmEngine === "INFERENCE_ENDPOINT" ? hfie : hf
|
| 43 |
|
| 44 |
-
export async function predictWithHuggingFace(inputs: string) {
|
| 45 |
let instructions = ""
|
| 46 |
try {
|
| 47 |
for await (const output of api.textGenerationStream({
|
|
|
|
| 3 |
import { HfInference, HfInferenceEndpoint } from "@huggingface/inference"
|
| 4 |
import { LLMEngine } from "@/types"
|
| 5 |
|
| 6 |
+
export async function predict(inputs: string): Promise<string> {
|
| 7 |
+
const hf = new HfInference(process.env.AUTH_HF_API_TOKEN)
|
| 8 |
|
| 9 |
+
const llmEngine = `${process.env.LLM_ENGINE || ""}` as LLMEngine
|
| 10 |
+
const inferenceEndpoint = `${process.env.LLM_HF_INFERENCE_ENDPOINT_URL || ""}`
|
| 11 |
+
const inferenceModel = `${process.env.LLM_HF_INFERENCE_API_MODEL || ""}`
|
| 12 |
|
| 13 |
+
let hfie: HfInferenceEndpoint = hf
|
| 14 |
|
| 15 |
+
switch (llmEngine) {
|
| 16 |
+
case "INFERENCE_ENDPOINT":
|
| 17 |
+
if (inferenceEndpoint) {
|
| 18 |
+
console.log("Using a custom HF Inference Endpoint")
|
| 19 |
+
hfie = hf.endpoint(inferenceEndpoint)
|
| 20 |
+
} else {
|
| 21 |
+
const error = "No Inference Endpoint URL defined"
|
| 22 |
+
console.error(error)
|
| 23 |
+
throw new Error(error)
|
| 24 |
+
}
|
| 25 |
+
break;
|
| 26 |
+
|
| 27 |
+
case "INFERENCE_API":
|
| 28 |
+
if (inferenceModel) {
|
| 29 |
+
console.log("Using an HF Inference API Model")
|
| 30 |
+
} else {
|
| 31 |
+
const error = "No Inference API model defined"
|
| 32 |
+
console.error(error)
|
| 33 |
+
throw new Error(error)
|
| 34 |
+
}
|
| 35 |
+
break;
|
| 36 |
+
|
| 37 |
+
default:
|
| 38 |
+
const error = "Please check your Hugging Face Inference API or Inference Endpoint settings"
|
| 39 |
console.error(error)
|
| 40 |
throw new Error(error)
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
const api = llmEngine === "INFERENCE_ENDPOINT" ? hfie : hf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
|
|
|
| 45 |
let instructions = ""
|
| 46 |
try {
|
| 47 |
for await (const output of api.textGenerationStream({
|
src/app/queries/predictWithOpenAI.ts
CHANGED
|
@@ -3,9 +3,8 @@
|
|
| 3 |
import type { ChatCompletionMessage } from "openai/resources/chat"
|
| 4 |
import OpenAI from "openai"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
export async function predictWithOpenAI(inputs: string) {
|
| 9 |
const openaiApiBaseUrl = `${process.env.LLM_OPENAI_API_BASE_URL || "https://api.openai.com/v1"}`
|
| 10 |
const openaiApiModel = `${process.env.LLM_OPENAI_API_MODEL || "gpt-3.5-turbo"}`
|
| 11 |
|
|
@@ -26,8 +25,9 @@ export async function predictWithOpenAI(inputs: string) {
|
|
| 26 |
temperature: 0.8
|
| 27 |
})
|
| 28 |
|
| 29 |
-
return res.choices[0].message.content
|
| 30 |
} catch (err) {
|
| 31 |
console.error(`error during generation: ${err}`)
|
|
|
|
| 32 |
}
|
| 33 |
}
|
|
|
|
| 3 |
import type { ChatCompletionMessage } from "openai/resources/chat"
|
| 4 |
import OpenAI from "openai"
|
| 5 |
|
| 6 |
+
export async function predict(inputs: string): Promise<string> {
|
| 7 |
+
const openaiApiKey = `${process.env.AUTH_OPENAI_API_KEY || ""}`
|
|
|
|
| 8 |
const openaiApiBaseUrl = `${process.env.LLM_OPENAI_API_BASE_URL || "https://api.openai.com/v1"}`
|
| 9 |
const openaiApiModel = `${process.env.LLM_OPENAI_API_MODEL || "gpt-3.5-turbo"}`
|
| 10 |
|
|
|
|
| 25 |
temperature: 0.8
|
| 26 |
})
|
| 27 |
|
| 28 |
+
return res.choices[0].message.content || ""
|
| 29 |
} catch (err) {
|
| 30 |
console.error(`error during generation: ${err}`)
|
| 31 |
+
return ""
|
| 32 |
}
|
| 33 |
}
|