Spaces:
Runtime error
Runtime error
Improve inference functions (#28)
Browse filesFiles changed (2)
128 unmodified lines1291301311321331341351321331341351 unmodified line137138139143140141142143144147148149150151152153145146147148128 unmodified lines
await handleStreamingResponse( hf, conversation.model, requestMessages, conversation.config.temperature, conversation.config.maxTokens, conversation, (content) => { if (streamingMessage) { streamingMessage.content = content;1 unmodified line conversations = conversations; } }, abortController abortController, systemMessage ); } else { waitForNonStreaming = true; const newMessage = await handleNonStreamingResponse( hf, conversation.model, requestMessages, conversation.config.temperature, conversation.config.maxTokens ); const newMessage = await handleNonStreamingResponse(hf, conversation, systemMessage); // check if the user did not abort the request if (waitForNonStreaming) { conversation.messages = [...conversation.messages, newMessage];12334561 unmodified line89101112131411121613141516171819202122232425222324252627282930313213 unmodified lines4647484546474849505152535455565758595152535460616263646566import { type ChatCompletionInputMessage } from '@huggingface/tasks';import { HfInference } from '@huggingface/inference';import type { ModelEntryWithTokenizer } from '$lib/types';import type { Conversation, ModelEntryWithTokenizer } from '$lib/types';
export function createHfInference(token: string): HfInference { return new HfInference(token);1 unmodified line
export async function handleStreamingResponse( hf: HfInference, model: string, messages: ChatCompletionInputMessage[], temperature: number, maxTokens: number, conversation: Conversation, onChunk: (content: string) => void, abortController: AbortController abortController: AbortController, systemMessage?: ChatCompletionInputMessage): Promise<void> { const messages = [ ...(isSystemPromptSupported(conversation.model) && systemMessage?.content?.length ? [systemMessage] : []), ...conversation.messages ]; let out = ''; try { for await (const chunk of hf.chatCompletionStream( { model: model, messages: messages, temperature: temperature, max_tokens: maxTokens model: conversation.model.id, messages, temperature: conversation.config.temperature, max_tokens: conversation.config.maxTokens }, { signal: abortController.signal } )) {13 unmodified lines
export async function handleNonStreamingResponse( hf: HfInference, model: string, messages: ChatCompletionInputMessage[], temperature: number, maxTokens: number conversation: Conversation, systemMessage?: ChatCompletionInputMessage): Promise<ChatCompletionInputMessage> { const messages = [ ...(isSystemPromptSupported(conversation.model) && systemMessage?.content?.length ? [systemMessage] : []), ...conversation.messages ];
const response = await hf.chatCompletion({ model: model, messages: messages, temperature: temperature, max_tokens: maxTokens model: conversation.model, messages, temperature: conversation.config.temperature, max_tokens: conversation.config.maxTokens });
if (response.choices && response.choices.length > 0) {src/lib/components/InferencePlayground/InferencePlayground.svelte+4-12
src/lib/components/InferencePlayground/inferencePlaygroundUtils.ts+27-18