mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-08-28 05:27:24 +00:00
basic hugging face provider
This commit is contained in:
parent
714fafd328
commit
be4fcfa9de
10 changed files with 189 additions and 0 deletions
|
|
@ -32,6 +32,7 @@ export const providerNames = [
|
|||
"groq",
|
||||
"chutes",
|
||||
"litellm",
|
||||
"huggingface",
|
||||
] as const
|
||||
|
||||
export const providerNamesSchema = z.enum(providerNames)
|
||||
|
|
@ -219,6 +220,11 @@ const groqSchema = apiModelIdProviderModelSchema.extend({
|
|||
groqApiKey: z.string().optional(),
|
||||
})
|
||||
|
||||
const huggingFaceSchema = baseProviderSettingsSchema.extend({
|
||||
huggingFaceApiKey: z.string().optional(),
|
||||
huggingFaceModelId: z.string().optional(),
|
||||
})
|
||||
|
||||
const chutesSchema = apiModelIdProviderModelSchema.extend({
|
||||
chutesApiKey: z.string().optional(),
|
||||
})
|
||||
|
|
@ -256,6 +262,7 @@ export const providerSettingsSchemaDiscriminated = z.discriminatedUnion("apiProv
|
|||
fakeAiSchema.merge(z.object({ apiProvider: z.literal("fake-ai") })),
|
||||
xaiSchema.merge(z.object({ apiProvider: z.literal("xai") })),
|
||||
groqSchema.merge(z.object({ apiProvider: z.literal("groq") })),
|
||||
huggingFaceSchema.merge(z.object({ apiProvider: z.literal("huggingface") })),
|
||||
chutesSchema.merge(z.object({ apiProvider: z.literal("chutes") })),
|
||||
litellmSchema.merge(z.object({ apiProvider: z.literal("litellm") })),
|
||||
defaultSchema,
|
||||
|
|
@ -285,6 +292,7 @@ export const providerSettingsSchema = z.object({
|
|||
...fakeAiSchema.shape,
|
||||
...xaiSchema.shape,
|
||||
...groqSchema.shape,
|
||||
...huggingFaceSchema.shape,
|
||||
...chutesSchema.shape,
|
||||
...litellmSchema.shape,
|
||||
...codebaseIndexProviderSchema.shape,
|
||||
|
|
@ -304,6 +312,7 @@ export const MODEL_ID_KEYS: Partial<keyof ProviderSettings>[] = [
|
|||
"unboundModelId",
|
||||
"requestyModelId",
|
||||
"litellmModelId",
|
||||
"huggingFaceModelId",
|
||||
]
|
||||
|
||||
export const getModelId = (settings: ProviderSettings): string | undefined => {
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import {
|
|||
FakeAIHandler,
|
||||
XAIHandler,
|
||||
GroqHandler,
|
||||
HuggingFaceHandler,
|
||||
ChutesHandler,
|
||||
LiteLLMHandler,
|
||||
ClaudeCodeHandler,
|
||||
|
|
@ -108,6 +109,8 @@ export function buildApiHandler(configuration: ProviderSettings): ApiHandler {
|
|||
return new XAIHandler(options)
|
||||
case "groq":
|
||||
return new GroqHandler(options)
|
||||
case "huggingface":
|
||||
return new HuggingFaceHandler(options)
|
||||
case "chutes":
|
||||
return new ChutesHandler(options)
|
||||
case "litellm":
|
||||
|
|
|
|||
99
src/api/providers/huggingface.ts
Normal file
99
src/api/providers/huggingface.ts
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
import OpenAI from "openai"
|
||||
import { Anthropic } from "@anthropic-ai/sdk"
|
||||
|
||||
import type { ApiHandlerOptions } from "../../shared/api"
|
||||
import { ApiStream } from "../transform/stream"
|
||||
import { convertToOpenAiMessages } from "../transform/openai-format"
|
||||
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index"
|
||||
import { DEFAULT_HEADERS } from "./constants"
|
||||
import { BaseProvider } from "./base-provider"
|
||||
|
||||
export class HuggingFaceHandler extends BaseProvider implements SingleCompletionHandler {
|
||||
private client: OpenAI
|
||||
private options: ApiHandlerOptions
|
||||
|
||||
constructor(options: ApiHandlerOptions) {
|
||||
super()
|
||||
this.options = options
|
||||
|
||||
if (!this.options.huggingFaceApiKey) {
|
||||
throw new Error("Hugging Face API key is required")
|
||||
}
|
||||
|
||||
this.client = new OpenAI({
|
||||
baseURL: "https://router.huggingface.co/v1",
|
||||
apiKey: this.options.huggingFaceApiKey,
|
||||
defaultHeaders: DEFAULT_HEADERS,
|
||||
})
|
||||
}
|
||||
|
||||
override async *createMessage(
|
||||
systemPrompt: string,
|
||||
messages: Anthropic.Messages.MessageParam[],
|
||||
metadata?: ApiHandlerCreateMessageMetadata,
|
||||
): ApiStream {
|
||||
const modelId = this.options.huggingFaceModelId || "meta-llama/Llama-3.3-70B-Instruct"
|
||||
const temperature = this.options.modelTemperature ?? 0.7
|
||||
|
||||
const params: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = {
|
||||
model: modelId,
|
||||
temperature,
|
||||
messages: [{ role: "system", content: systemPrompt }, ...convertToOpenAiMessages(messages)],
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
}
|
||||
|
||||
const stream = await this.client.chat.completions.create(params)
|
||||
|
||||
for await (const chunk of stream) {
|
||||
const delta = chunk.choices[0]?.delta
|
||||
|
||||
if (delta?.content) {
|
||||
yield {
|
||||
type: "text",
|
||||
text: delta.content,
|
||||
}
|
||||
}
|
||||
|
||||
if (chunk.usage) {
|
||||
yield {
|
||||
type: "usage",
|
||||
inputTokens: chunk.usage.prompt_tokens || 0,
|
||||
outputTokens: chunk.usage.completion_tokens || 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async completePrompt(prompt: string): Promise<string> {
|
||||
const modelId = this.options.huggingFaceModelId || "meta-llama/Llama-3.3-70B-Instruct"
|
||||
|
||||
try {
|
||||
const response = await this.client.chat.completions.create({
|
||||
model: modelId,
|
||||
messages: [{ role: "user", content: prompt }],
|
||||
})
|
||||
|
||||
return response.choices[0]?.message.content || ""
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
throw new Error(`Hugging Face completion error: ${error.message}`)
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
override getModel() {
|
||||
const modelId = this.options.huggingFaceModelId || "meta-llama/Llama-3.3-70B-Instruct"
|
||||
return {
|
||||
id: modelId,
|
||||
info: {
|
||||
maxTokens: 8192,
|
||||
contextWindow: 131072,
|
||||
supportsImages: false,
|
||||
supportsPromptCache: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ export { FakeAIHandler } from "./fake-ai"
|
|||
export { GeminiHandler } from "./gemini"
|
||||
export { GlamaHandler } from "./glama"
|
||||
export { GroqHandler } from "./groq"
|
||||
export { HuggingFaceHandler } from "./huggingface"
|
||||
export { HumanRelayHandler } from "./human-relay"
|
||||
export { LiteLLMHandler } from "./lite-llm"
|
||||
export { LmStudioHandler } from "./lm-studio"
|
||||
|
|
|
|||
|
|
@ -59,6 +59,7 @@ import {
|
|||
Gemini,
|
||||
Glama,
|
||||
Groq,
|
||||
HuggingFace,
|
||||
LMStudio,
|
||||
LiteLLM,
|
||||
Mistral,
|
||||
|
|
@ -487,6 +488,10 @@ const ApiOptions = ({
|
|||
<Groq apiConfiguration={apiConfiguration} setApiConfigurationField={setApiConfigurationField} />
|
||||
)}
|
||||
|
||||
{selectedProvider === "huggingface" && (
|
||||
<HuggingFace apiConfiguration={apiConfiguration} setApiConfigurationField={setApiConfigurationField} />
|
||||
)}
|
||||
|
||||
{selectedProvider === "chutes" && (
|
||||
<Chutes apiConfiguration={apiConfiguration} setApiConfigurationField={setApiConfigurationField} />
|
||||
)}
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ export const PROVIDERS = [
|
|||
{ value: "human-relay", label: "Human Relay" },
|
||||
{ value: "xai", label: "xAI (Grok)" },
|
||||
{ value: "groq", label: "Groq" },
|
||||
{ value: "huggingface", label: "Hugging Face" },
|
||||
{ value: "chutes", label: "Chutes AI" },
|
||||
{ value: "litellm", label: "LiteLLM" },
|
||||
].sort((a, b) => a.label.localeCompare(b.label))
|
||||
|
|
|
|||
57
webview-ui/src/components/settings/providers/HuggingFace.tsx
Normal file
57
webview-ui/src/components/settings/providers/HuggingFace.tsx
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import { useCallback } from "react"
|
||||
import { VSCodeTextField } from "@vscode/webview-ui-toolkit/react"
|
||||
|
||||
import type { ProviderSettings } from "@roo-code/types"
|
||||
|
||||
import { useAppTranslation } from "@src/i18n/TranslationContext"
|
||||
import { VSCodeButtonLink } from "@src/components/common/VSCodeButtonLink"
|
||||
|
||||
import { inputEventTransform } from "../transforms"
|
||||
|
||||
type HuggingFaceProps = {
|
||||
apiConfiguration: ProviderSettings
|
||||
setApiConfigurationField: (field: keyof ProviderSettings, value: ProviderSettings[keyof ProviderSettings]) => void
|
||||
}
|
||||
|
||||
export const HuggingFace = ({ apiConfiguration, setApiConfigurationField }: HuggingFaceProps) => {
|
||||
const { t } = useAppTranslation()
|
||||
|
||||
const handleInputChange = useCallback(
|
||||
<K extends keyof ProviderSettings, E>(
|
||||
field: K,
|
||||
transform: (event: E) => ProviderSettings[K] = inputEventTransform,
|
||||
) =>
|
||||
(event: E | Event) => {
|
||||
setApiConfigurationField(field, transform(event as E))
|
||||
},
|
||||
[setApiConfigurationField],
|
||||
)
|
||||
|
||||
return (
|
||||
<>
|
||||
<VSCodeTextField
|
||||
value={apiConfiguration?.huggingFaceApiKey || ""}
|
||||
type="password"
|
||||
onInput={handleInputChange("huggingFaceApiKey")}
|
||||
placeholder={t("settings:placeholders.apiKey")}
|
||||
className="w-full">
|
||||
<label className="block font-medium mb-1">{t("settings:providers.huggingFaceApiKey")}</label>
|
||||
</VSCodeTextField>
|
||||
<VSCodeTextField
|
||||
value={apiConfiguration?.huggingFaceModelId || ""}
|
||||
onInput={handleInputChange("huggingFaceModelId")}
|
||||
placeholder="meta-llama/Llama-3.3-70B-Instruct"
|
||||
className="w-full">
|
||||
<label className="block font-medium mb-1">{t("settings:providers.huggingFaceModelId")}</label>
|
||||
</VSCodeTextField>
|
||||
<div className="text-sm text-vscode-descriptionForeground -mt-2">
|
||||
{t("settings:providers.apiKeyStorageNotice")}
|
||||
</div>
|
||||
{!apiConfiguration?.huggingFaceApiKey && (
|
||||
<VSCodeButtonLink href="https://huggingface.co/settings/tokens" appearance="secondary">
|
||||
{t("settings:providers.getHuggingFaceApiKey")}
|
||||
</VSCodeButtonLink>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ export { DeepSeek } from "./DeepSeek"
|
|||
export { Gemini } from "./Gemini"
|
||||
export { Glama } from "./Glama"
|
||||
export { Groq } from "./Groq"
|
||||
export { HuggingFace } from "./HuggingFace"
|
||||
export { LMStudio } from "./LMStudio"
|
||||
export { Mistral } from "./Mistral"
|
||||
export { Moonshot } from "./Moonshot"
|
||||
|
|
|
|||
|
|
@ -130,6 +130,16 @@ function getSelectedModel({
|
|||
const info = groqModels[id as keyof typeof groqModels]
|
||||
return { id, info }
|
||||
}
|
||||
case "huggingface": {
|
||||
const id = apiConfiguration.huggingFaceModelId ?? "meta-llama/Llama-3.3-70B-Instruct"
|
||||
const info = {
|
||||
maxTokens: 8192,
|
||||
contextWindow: 131072,
|
||||
supportsImages: false,
|
||||
supportsPromptCache: false,
|
||||
}
|
||||
return { id, info }
|
||||
}
|
||||
case "chutes": {
|
||||
const id = apiConfiguration.apiModelId ?? chutesDefaultModelId
|
||||
const info = chutesModels[id as keyof typeof chutesModels]
|
||||
|
|
|
|||
|
|
@ -259,6 +259,9 @@
|
|||
"geminiApiKey": "Gemini API Key",
|
||||
"getGroqApiKey": "Get Groq API Key",
|
||||
"groqApiKey": "Groq API Key",
|
||||
"getHuggingFaceApiKey": "Get Hugging Face API Key",
|
||||
"huggingFaceApiKey": "Hugging Face API Key",
|
||||
"huggingFaceModelId": "Model ID",
|
||||
"getGeminiApiKey": "Get Gemini API Key",
|
||||
"openAiApiKey": "OpenAI API Key",
|
||||
"apiKey": "API Key",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue