Spaces:
Runtime error
Runtime error
Improve inference functions (#28)
Browse filesFiles changed (2)
128 unmodified lines1291301311321331341351361371381 unmodified line140141142143144145146147148149150151152153154155156128 unmodified linesawait handleStreamingResponse(hf,conversation.model,requestMessages,conversation.config.temperature,conversation.config.maxTokens,(content) => {if (streamingMessage) {streamingMessage.content = content;1 unmodified lineconversations = conversations;}},abortController);} else {waitForNonStreaming = true;const newMessage = await handleNonStreamingResponse(hf,conversation.model,requestMessages,conversation.config.temperature,conversation.config.maxTokens);// check if the user did not abort the requestif (waitForNonStreaming) {conversation.messages = [...conversation.messages, newMessage];128 unmodified lines1291301311321331341351 unmodified line137138139140141142143144145146147148128 unmodified linesawait handleStreamingResponse(hf,conversation,(content) => {if (streamingMessage) {streamingMessage.content = content;1 unmodified lineconversations = conversations;}},abortController,systemMessage);} else {waitForNonStreaming = true;const newMessage = await handleNonStreamingResponse(hf, conversation, systemMessage);// check if the user did not abort the requestif (waitForNonStreaming) {conversation.messages = [...conversation.messages, newMessage];
1234561 unmodified line891011121314151617181920212223242526272813 unmodified lines42434445464748495051525354555657import { type ChatCompletionInputMessage } from '@huggingface/tasks';import { HfInference } from '@huggingface/inference';import type { ModelEntryWithTokenizer } from '$lib/types';export function createHfInference(token: string): HfInference {return new HfInference(token);1 unmodified lineexport async function handleStreamingResponse(hf: HfInference,model: string,messages: ChatCompletionInputMessage[],temperature: number,maxTokens: number,onChunk: (content: string) => void,abortController: AbortController): Promise<void> {let out = '';try {for await (const chunk of hf.chatCompletionStream({model: model,messages: messages,temperature: temperature,max_tokens: maxTokens},{ signal: abortController.signal })) {13 unmodified linesexport async function handleNonStreamingResponse(hf: HfInference,model: string,messages: ChatCompletionInputMessage[],temperature: number,maxTokens: number): Promise<ChatCompletionInputMessage> {const response = await hf.chatCompletion({model: model,messages: messages,temperature: temperature,max_tokens: maxTokens});if (response.choices && response.choices.length > 0) {1234561 unmodified line89101112131415161718192021222324252627282930313213 unmodified lines464748495051525354555657585960616263646566import { type ChatCompletionInputMessage } from '@huggingface/tasks';import { HfInference } from '@huggingface/inference';import type { Conversation, ModelEntryWithTokenizer } from '$lib/types';export function createHfInference(token: string): HfInference {return new HfInference(token);1 unmodified lineexport async function handleStreamingResponse(hf: HfInference,conversation: Conversation,onChunk: (content: string) => void,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: conversation.model.id,messages,temperature: conversation.config.temperature,max_tokens: conversation.config.maxTokens},{ signal: abortController.signal })) {13 unmodified linesexport async function handleNonStreamingResponse(hf: HfInference,conversation: Conversation,systemMessage?: ChatCompletionInputMessage): Promise<ChatCompletionInputMessage> {const messages = [...(isSystemPromptSupported(conversation.model) && systemMessage?.content?.length? [systemMessage]: []),...conversation.messages];const response = await hf.chatCompletion({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