✨ set custom headers

This commit is contained in:
Nate Sesti 2023-12-13 21:20:54 -08:00
parent ada6799b3b
commit 35d7e199af
11 changed files with 36 additions and 16 deletions

View file

@ -222,6 +222,9 @@ export abstract class LLM implements LLMOptions {
this.llmRequestHook = options.llmRequestHook;
this.apiKey = options.apiKey;
this.apiBase = options.apiBase;
if (this.apiBase?.endsWith("/")) {
this.apiBase = this.apiBase.slice(0, -1);
}
this.engine = options.engine;
this.apiVersion = options.apiVersion;
@ -300,6 +303,20 @@ export abstract class LLM implements LLMOptions {
// });
}
protected fetch(url: string, init?: RequestInit): Promise<Response> {
const headers = new Headers(init?.headers);
for (const [key, value] of Object.entries(
this.requestOptions?.headers || {}
)) {
headers.append(key, value as string);
}
return fetch(url, {
...init,
headers,
});
}
private _parseCompletionOptions(options: LLMFullCompletionOptions) {
const log = options.log ?? true;
const raw = options.raw ?? false;

View file

@ -46,7 +46,7 @@ class Anthropic extends LLM {
prompt: string,
options: CompletionOptions
): Promise<string> {
const response = await fetch(this.apiBase + "/complete", {
const response = await this.fetch(this.apiBase + "/complete", {
method: "POST",
headers: {
"Content-Type": "application/json",
@ -67,7 +67,7 @@ class Anthropic extends LLM {
prompt: string,
options: CompletionOptions
): AsyncGenerator<string> {
const response = await fetch(this.apiBase + "/complete", {
const response = await this.fetch(this.apiBase + "/complete", {
method: "POST",
headers: {
"Content-Type": "application/json",

View file

@ -21,7 +21,7 @@ class FreeTrial extends LLM {
): Promise<string> {
const args = this.collectArgs(options);
const response = await fetch(`${SERVER_URL}/complete`, {
const response = await this.fetch(`${SERVER_URL}/complete`, {
method: "POST",
headers: this._getHeaders(),
body: JSON.stringify({
@ -39,7 +39,7 @@ class FreeTrial extends LLM {
): AsyncGenerator<string> {
const args = this.collectArgs(options);
const response = await fetch(`${SERVER_URL}/stream_complete`, {
const response = await this.fetch(`${SERVER_URL}/stream_complete`, {
method: "POST",
headers: this._getHeaders(),
body: JSON.stringify({
@ -69,7 +69,7 @@ class FreeTrial extends LLM {
): AsyncGenerator<ChatMessage> {
const args = this.collectArgs(options);
const response = await fetch(`${SERVER_URL}/stream_chat`, {
const response = await this.fetch(`${SERVER_URL}/stream_chat`, {
method: "POST",
headers: this._getHeaders(),
body: JSON.stringify({

View file

@ -43,7 +43,7 @@ class Gemini extends LLM {
stopSequences: options.stop,
},
};
const response = await fetch(apiUrl, {
const response = await this.fetch(apiUrl, {
method: "POST",
body: JSON.stringify(body),
headers: {

View file

@ -49,7 +49,7 @@ class GooglePalm extends LLM {
};
}),
};
const response = await fetch(apiURL, {
const response = await this.fetch(apiURL, {
method: "POST",
body: JSON.stringify(body),
});
@ -73,7 +73,7 @@ class GooglePalm extends LLM {
const apiURL = `https://generativelanguage.googleapis.com/v1beta2/models/${options.model}:generateMessage?key=${this.apiKey}`;
const body = { prompt: { messages: msgList } };
const response = await fetch(apiURL, {
const response = await this.fetch(apiURL, {
method: "POST",
body: JSON.stringify(body),
});

View file

@ -1,6 +1,6 @@
import { LLM, LLMOptions } from "..";
import { ModelProvider } from "../../config";
import { ChatMessage, CompletionOptions } from "../types";
import { CompletionOptions } from "../types";
class HuggingFaceTGI extends LLM {
static providerName: ModelProvider = "huggingface-tgi";
@ -29,7 +29,7 @@ class HuggingFaceTGI extends LLM {
): AsyncGenerator<string> {
const args = this._convertArgs(options, prompt);
const response = await fetch(`${this.apiBase}/generate_stream`, {
const response = await this.fetch(`${this.apiBase}/generate_stream`, {
method: "POST",
headers: {
"Content-Type": "application/json",

View file

@ -27,7 +27,7 @@ class LlamaCpp extends LLM {
...this.requestOptions?.headers,
};
const resp = await fetch(`${this.apiBase}/completion`, {
const resp = await this.fetch(`${this.apiBase}/completion`, {
method: "POST",
headers,
body: JSON.stringify({
@ -49,7 +49,11 @@ class LlamaCpp extends LLM {
buffer = lines.pop() ?? "";
for (const line of lines) {
if (line.trim() === "") continue;
yield JSON.parse(line.substring(6))["content"];
const data = JSON.parse(line.substring(6));
if ("error" in data) {
throw new Error(data.error);
}
yield data.content;
}
}

View file

@ -1,4 +1,3 @@
import { LLM } from "..";
import { ModelProvider } from "../../config";
import LlamaCpp from "./LlamaCpp";

View file

@ -53,7 +53,7 @@ class Ollama extends LLM {
prompt: string,
options: CompletionOptions
): AsyncGenerator<string> {
const response = await fetch(`${this.apiBase}/api/generate`, {
const response = await this.fetch(`${this.apiBase}/api/generate`, {
method: "POST",
headers: {
"Content-Type": "application/json",

View file

@ -65,7 +65,7 @@ class OpenAI extends LLM {
messages: ChatMessage[],
options: CompletionOptions
): AsyncGenerator<ChatMessage> {
const response = await fetch(this._getChatUrl(), {
const response = await this.fetch(this._getChatUrl(), {
method: "POST",
headers: {
"Content-Type": "application/json",

View file

@ -42,7 +42,7 @@ class Together extends LLM {
prompt: string,
options: CompletionOptions
): AsyncGenerator<string> {
const response = await fetch(`${this.apiBase}/inference`, {
const response = await this.fetch(`${this.apiBase}/inference`, {
method: "POST",
headers: {
"Content-Type": "application/json",