This commit is contained in:
alex 2023-10-29 02:58:25 +00:00
parent 06d3264b15
commit 6d6c7e0bb3
7 changed files with 147 additions and 28 deletions

99
package-lock.json generated
View file

@ -11,6 +11,9 @@
"dependencies": {
"@emotion/react": "^11.10.6",
"@emotion/server": "^11.10.0",
"@huggingface/agents": "^0.0.5",
"@huggingface/hub": "^0.11.3",
"@huggingface/inference": "^2.6.4",
"@mantine/core": "^6.0.1",
"@mantine/hooks": "^6.0.1",
"@mantine/next": "^6.0.1",
@ -23,6 +26,7 @@
"dexie": "^3.2.3",
"dexie-export-import": "^4.0.6",
"dexie-react-hooks": "^1.1.3",
"dotenv": "^16.3.1",
"downloadjs": "^1.4.7",
"eslint": "8.36.0",
"eslint-config-next": "13.2.4",
@ -405,6 +409,36 @@
"react-dom": ">=16.8.0"
}
},
"node_modules/@huggingface/agents": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/@huggingface/agents/-/agents-0.0.5.tgz",
"integrity": "sha512-h3mutXX0sFg4o9ZDHh3gzsF+qydcrTW9MVll2g/IVrNiQ0pSfXZqOQPRnfd85XU49buY3Zd1oHUDI0Bz8PznJw==",
"dependencies": {
"@huggingface/inference": "^2.6.1"
},
"engines": {
"node": ">=18"
}
},
"node_modules/@huggingface/hub": {
"version": "0.11.3",
"resolved": "https://registry.npmjs.org/@huggingface/hub/-/hub-0.11.3.tgz",
"integrity": "sha512-GpdW09CQNgmZsSvVDlh7A1Vrr1NSfeEcAdtvRPaF9Cs6q2/6fywK0HInZHgrbPcXuawsnoIFvuePkboTol2Ttg==",
"dependencies": {
"hash-wasm": "^4.9.0"
},
"engines": {
"node": ">=18"
}
},
"node_modules/@huggingface/inference": {
"version": "2.6.4",
"resolved": "https://registry.npmjs.org/@huggingface/inference/-/inference-2.6.4.tgz",
"integrity": "sha512-Xna7arltBSBoKaH3diGi3sYvkExgJMd/pF4T6vl2YbmDccbr1G/X5EPZ2048p+YgrJYG1jTYFCtY6Dr3HvJaow==",
"engines": {
"node": ">=18"
}
},
"node_modules/@humanwhocodes/config-array": {
"version": "0.11.8",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.8.tgz",
@ -881,6 +915,15 @@
"url": "https://opencollective.com/parcel"
}
},
"node_modules/@parcel/core/node_modules/dotenv": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-7.0.0.tgz",
"integrity": "sha512-M3NhsLbV1i6HuGzBUH8vXrtxOk+tWmzWKDMbAVSUp3Zsjm7ywFeuwrUXhmhQyRK1q5B5GGy7hcXPbj3bnfZg2g==",
"dev": true,
"engines": {
"node": ">=6"
}
},
"node_modules/@parcel/core/node_modules/semver": {
"version": "5.7.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.2.tgz",
@ -3206,12 +3249,14 @@
}
},
"node_modules/dotenv": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-7.0.0.tgz",
"integrity": "sha512-M3NhsLbV1i6HuGzBUH8vXrtxOk+tWmzWKDMbAVSUp3Zsjm7ywFeuwrUXhmhQyRK1q5B5GGy7hcXPbj3bnfZg2g==",
"dev": true,
"version": "16.3.1",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz",
"integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==",
"engines": {
"node": ">=6"
"node": ">=12"
},
"funding": {
"url": "https://github.com/motdotla/dotenv?sponsor=1"
}
},
"node_modules/dotenv-expand": {
@ -4279,6 +4324,11 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/hash-wasm": {
"version": "4.10.0",
"resolved": "https://registry.npmjs.org/hash-wasm/-/hash-wasm-4.10.0.tgz",
"integrity": "sha512-a0NjBNWjavvMalm/pPSEJ00MPDjRG8rv9D5BK7dBQTLGwAOVWqnTEUggaYs5szATB5UK5ULeIQr7QJXbczAZYA=="
},
"node_modules/hast-util-whitespace": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/hast-util-whitespace/-/hast-util-whitespace-2.0.1.tgz",
@ -8330,6 +8380,27 @@
"@floating-ui/dom": "^1.2.1"
}
},
"@huggingface/agents": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/@huggingface/agents/-/agents-0.0.5.tgz",
"integrity": "sha512-h3mutXX0sFg4o9ZDHh3gzsF+qydcrTW9MVll2g/IVrNiQ0pSfXZqOQPRnfd85XU49buY3Zd1oHUDI0Bz8PznJw==",
"requires": {
"@huggingface/inference": "^2.6.1"
}
},
"@huggingface/hub": {
"version": "0.11.3",
"resolved": "https://registry.npmjs.org/@huggingface/hub/-/hub-0.11.3.tgz",
"integrity": "sha512-GpdW09CQNgmZsSvVDlh7A1Vrr1NSfeEcAdtvRPaF9Cs6q2/6fywK0HInZHgrbPcXuawsnoIFvuePkboTol2Ttg==",
"requires": {
"hash-wasm": "^4.9.0"
}
},
"@huggingface/inference": {
"version": "2.6.4",
"resolved": "https://registry.npmjs.org/@huggingface/inference/-/inference-2.6.4.tgz",
"integrity": "sha512-Xna7arltBSBoKaH3diGi3sYvkExgJMd/pF4T6vl2YbmDccbr1G/X5EPZ2048p+YgrJYG1jTYFCtY6Dr3HvJaow=="
},
"@humanwhocodes/config-array": {
"version": "0.11.8",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.8.tgz",
@ -8669,6 +8740,12 @@
"semver": "^5.7.1"
},
"dependencies": {
"dotenv": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-7.0.0.tgz",
"integrity": "sha512-M3NhsLbV1i6HuGzBUH8vXrtxOk+tWmzWKDMbAVSUp3Zsjm7ywFeuwrUXhmhQyRK1q5B5GGy7hcXPbj3bnfZg2g==",
"dev": true
},
"semver": {
"version": "5.7.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.2.tgz",
@ -10243,10 +10320,9 @@
}
},
"dotenv": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-7.0.0.tgz",
"integrity": "sha512-M3NhsLbV1i6HuGzBUH8vXrtxOk+tWmzWKDMbAVSUp3Zsjm7ywFeuwrUXhmhQyRK1q5B5GGy7hcXPbj3bnfZg2g==",
"dev": true
"version": "16.3.1",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz",
"integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ=="
},
"dotenv-expand": {
"version": "5.1.0",
@ -11052,6 +11128,11 @@
"has-symbols": "^1.0.2"
}
},
"hash-wasm": {
"version": "4.10.0",
"resolved": "https://registry.npmjs.org/hash-wasm/-/hash-wasm-4.10.0.tgz",
"integrity": "sha512-a0NjBNWjavvMalm/pPSEJ00MPDjRG8rv9D5BK7dBQTLGwAOVWqnTEUggaYs5szATB5UK5ULeIQr7QJXbczAZYA=="
},
"hast-util-whitespace": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/hast-util-whitespace/-/hast-util-whitespace-2.0.1.tgz",

View file

@ -25,6 +25,9 @@
"dependencies": {
"@emotion/react": "^11.10.6",
"@emotion/server": "^11.10.0",
"@huggingface/agents": "^0.0.5",
"@huggingface/hub": "^0.11.3",
"@huggingface/inference": "^2.6.4",
"@mantine/core": "^6.0.1",
"@mantine/hooks": "^6.0.1",
"@mantine/next": "^6.0.1",
@ -37,6 +40,7 @@
"dexie": "^3.2.3",
"dexie-export-import": "^4.0.6",
"dexie-react-hooks": "^1.1.3",
"dotenv": "^16.3.1",
"downloadjs": "^1.4.7",
"eslint": "8.36.0",
"eslint-config-next": "13.2.4",

0
src/.env Normal file
View file

View file

@ -13,6 +13,11 @@ import {
import { ChatRoute } from "../routes/ChatRoute";
import { IndexRoute } from "../routes/IndexRoute";
import { Layout } from "./Layout";
import { createRepo, uploadFile } from "@huggingface/hub";
const HF_ACCESS_TOKEN = "hf_..."; // Ihr Hugging Face Zugriffstoken
const history = createHashHistory();
const location = new ReactLocation({ history });

View file

@ -1,6 +1,6 @@
interface Config {
defaultModel: AvailableModel["value"];
defaultType: 'openai' | 'custom';
defaultType: 'openai' | 'custom' | 'huggingface'; // Hinzugefügt 'huggingface' als Option
defaultAuth: 'none' | 'bearer-token' | 'api-key';
defaultBase: string;
defaultVersion: string;
@ -22,12 +22,12 @@ interface AvailableModel {
value: string;
label: string;
}
interface WritingCharacter {
label: string;
value: string;
}
interface WritingFormat {
value: string;
label: string;

View file

@ -0,0 +1,23 @@
// huggingfaceInference.ts
import { HfInference } from '@huggingface/inference';
const Hf = new HfInference(process.env.HUGGINGFACE_API_KEY);
export async function generateText(input: string) {
const generator = await Hf.textGenerationStream({
model: 'oasst-sft-4-pythia-12b-epoch-3.5',
inputs: input,
parameters: {
max_new_tokens: 200,
repetition_penalty: 1,
truncate: 1000,
},
});
let generatedText = '';
for await (const chunk of generator) {
generatedText += chunk.generated_text;
}
return generatedText;
}

View file

@ -3,6 +3,7 @@ import { ChatCompletionRequestMessage, Configuration, OpenAIApi } from "openai";
import { OpenAIExt } from "openai-ext";
import { db } from "../db";
import { config } from "./config";
import { generateText } from "./huggingfaceInference"; // Importieren Sie die Huggingface-Funktion
function getClient(
apiKey: string,
@ -83,23 +84,28 @@ export async function createChatCompletion(
const base = settings?.openAiApiBase ?? config.defaultBase;
const version = settings?.openAiApiVersion ?? config.defaultVersion;
const client = getClient(apiKey, type, auth, base);
return client.createChatCompletion(
{
model,
stream: false,
messages,
},
{
headers: {
"Content-Type": "application/json",
...(type === "custom" && auth === "api-key" && { "api-key": apiKey }),
if (type === "huggingface") {
const input = messages.map(message => message.content).join(" ");
return generateText(input);
} else {
const client = getClient(apiKey, type, auth, base);
return client.createChatCompletion(
{
model,
stream: false,
messages,
},
params: {
...(type === "custom" && { "api-version": version }),
},
}
);
{
headers: {
"Content-Type": "application/json",
...(type === "custom" && auth === "api-key" && { "api-key": apiKey }),
},
params: {
...(type === "custom" && { "api-version": version }),
},
}
);
}
}
export async function checkOpenAIKey(apiKey: string) {