continue-custom/core/llm/llms/HuggingFaceInferenceAPI.ts
Nate Sesti 34b40f4f53
Dev (#1627)
* docs: add docs and schema for "OS" provider (#1536)

* ignore .env

* ✨ use and cache imports for autocomplete (#1456)

* ✨ use and cache imports for autocomplete

* fix tsc

* add voyage rerank-1

* import Handlebars

* feat: open pane on install (#1564)

* feat: open pane on activation

* comment out testing code

* chore: add telemetry for pageviews (#1576)

* feat: update onboarding w/ embeddings model (#1570)

* chore(gui): remove unused pages

* feat: add embeddings step

* feat: update styles

* feat: copy button updates

* fix: correct pull command for embed model

* fix: remove commented code

* fix: remove commented code

* feat: simplify copy btn props

* chore: rename onboarding selection event

* feat: add provider config

* fix: undo msg name

* remove dead code

* fix: invalid mode check

* fix: remove testing logic

* fix: fullscreen gui retains context when hidden, fixed fullscreen focusing (#1582)

* small UI tweaks

* media query

* feat: add best experience onboarding

* small fixes

* feat: add free trial card to onboarding (#1600)

* feat: add free trial card to onboarding

* add import

* chore: add telemetry for full screen toggle (#1618)

* rerank-lite-1

* remove doccs

* basic tests for VS Code extension

* improved testing of VS Code extension

* manually implement stop tokens for hf inference api

* chore: onboarding metrics (#1626)

* fix: pageview tracking

* feat: add onboarding telemetry

* create single `onboardingStatus` type

* improved var naming

* remove console logs

* fix windows performance issue

* rename vscodeExtension.ts

* migration of onboarding variables

---------

Co-authored-by: Patrick Erichsen <patrick.a.erichsen@gmail.com>
Co-authored-by: Jonah Wagner <jonahwagner231@gmail.com>
2024-07-01 21:34:06 -07:00

59 lines
1.6 KiB
TypeScript

import { stopAtStopTokens } from "../../autocomplete/charStream.js";
import { CompletionOptions, ModelProvider } from "../../index.js";
import { BaseLLM } from "../index.js";
import { streamSse } from "../stream.js";
class HuggingFaceInferenceAPI extends BaseLLM {
static providerName: ModelProvider = "huggingface-inference-api";
private _convertArgs(options: CompletionOptions) {
return {
max_new_tokens: options.maxTokens ?? 1024,
temperature: options.temperature,
top_k: options.topK,
top_p: options.topP,
};
}
protected async *_streamComplete(
prompt: string,
options: CompletionOptions,
): AsyncGenerator<string> {
if (!this.apiBase) {
throw new Error(
"No API base URL provided. Please add the `apiBase` field in your config.json.",
);
}
const response = await this.fetch(this.apiBase, {
headers: {
Authorization: `Bearer ${this.apiKey}`,
"Content-Type": "application/json",
Accept: "application/json",
},
method: "POST",
body: JSON.stringify({
inputs: prompt,
stream: true,
parameters: this._convertArgs(options),
}),
});
async function* stream() {
for await (const chunk of streamSse(response)) {
const text = chunk?.token?.text ?? "";
if (text.endsWith("</s>")) {
yield text.slice(0, -5);
} else {
yield text;
}
}
}
for await (const text of stopAtStopTokens(stream(), options.stop ?? [])) {
yield text;
}
}
}
export default HuggingFaceInferenceAPI;