Skip to content

Commit 73a5c0d

Browse files
flexcharnsarrazin
andauthored
Several QoL contributions (#760)
* allow customizing disclaimer as `PUBLIC_APP_DISCLAIMER_MESSAGE` * support passing `defaultHeaders` to `openai` endpoint * add azure openai, claude, mistral examples using `defaultHeaders` & `openai` endpoint * fix streaming being buffered behind cloudflare tunnel might help to relieve issue #598 * support new lines in model description * don't automatically generate modelUrl to huggingface fixes broken links for self-hosted or custom-named model * add `PUBLIC_APP_DISCLAIMER_MESSAGE` in `.env` * `npm run format` --------- Co-authored-by: Nathan Sarrazin <sarrazin.nathan@gmail.com>
1 parent bbbedb7 commit 73a5c0d

File tree

7 files changed

+98
-15
lines changed

7 files changed

+98
-15
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ PUBLIC_APP_COLOR=blue # can be any of tailwind colors: https://tailwindcss.com/d
120120
PUBLIC_APP_DESCRIPTION=# description used throughout the app (if not set, a default one will be used)
121121
PUBLIC_APP_DATA_SHARING=#set to 1 to enable options & text regarding data sharing
122122
PUBLIC_APP_DISCLAIMER=#set to 1 to show a disclaimer on login page
123+
PUBLIC_APP_DISCLAIMER_MESSAGE="Disclaimer: AI is an area of active research with known problems such as biased generation and misinformation. Do not use this application for high-stakes decisions or advice."
123124
LLM_SUMMERIZATION=true
124125

125126
EXPOSE_API=true

.env.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ PUBLIC_APP_NAME=HuggingChat
228228
PUBLIC_APP_ASSETS=huggingchat
229229
PUBLIC_APP_COLOR=yellow
230230
PUBLIC_APP_DESCRIPTION="Making the community's best AI chat models available to everyone."
231+
PUBLIC_APP_DISCLAIMER_MESSAGE="Disclaimer: AI is an area of active research with known problems such as biased generation and misinformation. Do not use this application for high-stakes decisions or advice."
231232
PUBLIC_APP_DATA_SHARING=1
232233
PUBLIC_APP_DISCLAIMER=1
233234

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,75 @@ MODELS=`[{
316316
}]`
317317
```
318318

319+
You may also consume any model provider that provides compatible OpenAI API endpoint. For example, you may self-host [Portkey](https://github.com/Portkey-AI/gateway) gateway and experiment with Claude or GPTs offered by Azure OpenAI. Example for Claude from Anthropic:
320+
321+
```
322+
MODELS=`[{
323+
"name": "claude-2.1",
324+
"displayName": "Claude 2.1",
325+
"description": "Anthropic has been founded by former OpenAI researchers...",
326+
"parameters": {
327+
"temperature": 0.5,
328+
"max_new_tokens": 4096,
329+
},
330+
"endpoints": [
331+
{
332+
"type": "openai",
333+
"baseURL": "https://gateway.example.com/v1",
334+
"defaultHeaders": {
335+
"x-portkey-config": '{"provider":"anthropic","api_key":"sk-ant-abc...xyz"}'
336+
}
337+
}
338+
]
339+
}]`
340+
```
341+
342+
Example for GPT 4 deployed on Azure OpenAI:
343+
344+
```
345+
MODELS=`[{
346+
"id": "gpt-4-1106-preview",
347+
"name": "gpt-4-1106-preview",
348+
"displayName": "gpt-4-1106-preview",
349+
"parameters": {
350+
"temperature": 0.5,
351+
"max_new_tokens": 4096,
352+
},
353+
"endpoints": [
354+
{
355+
"type": "openai",
356+
"baseURL": "https://gateway.example.com/v1",
357+
"defaultHeaders": {
358+
"x-portkey-config": '{"provider":"azure-openai","resource_name":"abc-fr","deployment_id":"gpt-4-1106-preview","api_version":"2023-03-15-preview","api_key":"abc...xyz"}'
359+
}
360+
}
361+
]
362+
}]`
363+
```
364+
365+
Or try Mistral from [Deepinfra](https://deepinfra.com/mistralai/Mistral-7B-Instruct-v0.1/api?example=openai-http):
366+
367+
> Note, apiKey can either be set custom per endpoint, or globally using `OPENAI_API_KEY` variable.
368+
369+
```
370+
MODELS=`[{
371+
"name": "mistral-7b",
372+
"displayName": "Mistral 7B",
373+
"description": "A 7B dense Transformer, fast-deployed and easily customisable. Small, yet powerful for a variety of use cases. Supports English and code, and a 8k context window.",
374+
"parameters": {
375+
"temperature": 0.5,
376+
"max_new_tokens": 4096,
377+
},
378+
"endpoints": [
379+
{
380+
"type": "openai",
381+
"baseURL": "https://api.deepinfra.com/v1/openai",
382+
"apiKey": "abc...xyz"
383+
}
384+
]
385+
}]`
386+
```
387+
319388
##### Llama.cpp API server
320389

321390
chat-ui also supports the llama.cpp API server directly without the need for an adapter. You can do this using the `llamacpp` endpoint type.

src/lib/components/DisclaimerModal.svelte

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<script lang="ts">
22
import { base } from "$app/paths";
33
import { page } from "$app/stores";
4-
import { PUBLIC_APP_DESCRIPTION, PUBLIC_APP_NAME } from "$env/static/public";
4+
import {
5+
PUBLIC_APP_DESCRIPTION,
6+
PUBLIC_APP_NAME,
7+
PUBLIC_APP_DISCLAIMER_MESSAGE,
8+
} from "$env/static/public";
59
import LogoHuggingFaceBorderless from "$lib/components/icons/LogoHuggingFaceBorderless.svelte";
610
import Modal from "$lib/components/Modal.svelte";
711
import { useSettingsStore } from "$lib/stores/settings";
@@ -25,8 +29,7 @@
2529
</p>
2630

2731
<p class="text-sm text-gray-500">
28-
Disclaimer: AI is an area of active research with known problems such as biased generation and
29-
misinformation. Do not use this application for high-stakes decisions or advice.
32+
{PUBLIC_APP_DISCLAIMER_MESSAGE}
3033
</p>
3134

3235
<div class="flex w-full flex-col items-center gap-2">

src/lib/server/endpoints/openai/endpointOai.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ export const endpointOAIParametersSchema = z.object({
1515
completion: z
1616
.union([z.literal("completions"), z.literal("chat_completions")])
1717
.default("chat_completions"),
18+
defaultHeaders: z.record(z.string()).optional(),
1819
});
1920

2021
export async function endpointOai(
2122
input: z.input<typeof endpointOAIParametersSchema>
2223
): Promise<Endpoint> {
23-
const { baseURL, apiKey, completion, model } = endpointOAIParametersSchema.parse(input);
24+
const { baseURL, apiKey, completion, model, defaultHeaders } =
25+
endpointOAIParametersSchema.parse(input);
2426
let OpenAI;
2527
try {
2628
OpenAI = (await import("openai")).OpenAI;
@@ -31,6 +33,7 @@ export async function endpointOai(
3133
const openai = new OpenAI({
3234
apiKey: apiKey ?? "sk-",
3335
baseURL,
36+
defaultHeaders,
3437
});
3538

3639
if (completion === "completions") {

src/routes/conversation/[id]/+server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,11 @@ export async function POST({ request, locals, params, getClientAddress }) {
381381
});
382382

383383
// Todo: maybe we should wait for the message to be saved before ending the response - in case of errors
384-
return new Response(stream);
384+
return new Response(stream, {
385+
headers: {
386+
"Content-Type": "text/event-stream",
387+
},
388+
});
385389
}
386390

387391
export async function DELETE({ locals, params }) {

src/routes/settings/[...model]/+page.svelte

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,24 @@
3434
</h2>
3535

3636
{#if model.description}
37-
<p class=" text-gray-600">
37+
<p class="whitespace-pre-wrap text-gray-600">
3838
{model.description}
3939
</p>
4040
{/if}
4141
</div>
4242

4343
<div class="flex flex-wrap items-center gap-2 md:gap-4">
44-
<a
45-
href={model.modelUrl || "https://huggingface.co/" + model.name}
46-
target="_blank"
47-
rel="noreferrer"
48-
class="flex items-center truncate underline underline-offset-2"
49-
>
50-
<CarbonArrowUpRight class="mr-1.5 shrink-0 text-xs " />
51-
Model page
52-
</a>
44+
{#if model.modelUrl}
45+
<a
46+
href={model.modelUrl || "https://huggingface.co/" + model.name}
47+
target="_blank"
48+
rel="noreferrer"
49+
class="flex items-center truncate underline underline-offset-2"
50+
>
51+
<CarbonArrowUpRight class="mr-1.5 shrink-0 text-xs " />
52+
Model page
53+
</a>
54+
{/if}
5355

5456
{#if model.datasetName || model.datasetUrl}
5557
<a

0 commit comments

Comments
 (0)