Spaces:
Runtime error
Runtime error
Support python snippets (#20)
Browse files32 unmodified lines333435363738394024 unmodified lines656667686970717214 unmodified lines87888990919293945 unmodified lines10010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314432 unmodified lineslet selectedLanguage: Language = 'javascript';function getMessages(){const placeholder = [{ role: "user", content: "Tell me a story" }];const messages = conversation.messages.length ? conversation.messages : placeholder;return JSON.stringify(messages, null, 2);}24 unmodified linesfor await (const chunk of inference.chatCompletionStream({model: "${conversation.model}",messages: ${messagesStr},max_tokens: ${conversation.config.maxTokens},temperature: ${conversation.config.temperature},seed: 0,})) {if (chunk.choices && chunk.choices.length > 0) {14 unmodified linesconst out = await inference.chatCompletion({model: "${conversation.model}",messages: ${messagesStr},max_tokens: ${conversation.config.maxTokens},temperature: ${conversation.config.temperature},seed: 0,});5 unmodified lines}function getPythonSnippets() {const snippets: Snippet[] = [];snippets.push({label: 'Install',code: `import { HfInference } from '@huggingface/inference'const hf = new HfInference('your access token')`});if (conversation.config.streaming) {snippets.push({label: 'Streaming API',code: `let out = "";for await (const chunk of hf.chatCompletionStream({model: "${conversation.model}",messages: [{ role: "user", content: "Complete the equation 1+1= ,just the answer" },],max_tokens: ${conversation.config.maxTokens},temperature: ${conversation.config.temperature},seed: 0,})) {if (chunk.choices && chunk.choices.length > 0) {out += chunk.choices[0].delta.content;}}`});} else {// non-streamingsnippets.push({label: 'Non-Streaming API',code: `await hf.chatCompletion({model: "${conversation.model}",messages: [{ role: "user", content: "Complete the this sentence with words one plus one is equal " }],max_tokens: ${conversation.config.maxTokens},temperature: ${conversation.config.temperature},seed: 0,});`});}32 unmodified lines333435363738394024 unmodified lines656667686970717214 unmodified lines87888990919293945 unmodified lines10010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414532 unmodified lineslet selectedLanguage: Language = 'javascript';function getMessages() {const placeholder = [{ role: 'user', content: 'Tell me a story' }];const messages = conversation.messages.length ? conversation.messages : placeholder;return JSON.stringify(messages, null, 2);}24 unmodified linesfor await (const chunk of inference.chatCompletionStream({model: "${conversation.model}",messages: ${messagesStr},temperature: ${conversation.config.temperature},max_tokens: ${conversation.config.maxTokens},seed: 0,})) {if (chunk.choices && chunk.choices.length > 0) {14 unmodified linesconst out = await inference.chatCompletion({model: "${conversation.model}",messages: ${messagesStr},temperature: ${conversation.config.temperature},max_tokens: ${conversation.config.maxTokens},seed: 0,});5 unmodified lines}function getPythonSnippets() {const messagesStr = getMessages();const snippets: Snippet[] = [];snippets.push({label: 'Install huggingface_hub',language: 'bash',code: `pip install huggingface_hub`});if (conversation.config.streaming) {snippets.push({label: 'Streaming API',code: `from huggingface_hub import InferenceClientmodel_id="${conversation.model}"hf_token = "your HF token"inference_client = InferenceClient(model_id, token=hf_token)output = ""messages = ${messagesStr}for token in inference_client.chat_completion(messages, stream=True, temperature=${conversation.config.temperature}, max_tokens=${conversation.config.maxTokens}):new_content = token.choices[0].delta.contentprint(new_content, end="")output += new_content`});} else {// non-streamingsnippets.push({label: 'Non-Streaming API',code: `from huggingface_hub import InferenceClientmodel_id="${conversation.model}"hf_token = "your HF token"inference_client = InferenceClient(model_id, token=hf_token)messages = ${messagesStr}output = inference_client.chat_completion(messages, temperature=${conversation.config.temperature}, max_tokens=${conversation.config.maxTokens})print(output.choices[0].message)`});}
src/lib/components/CodeSnippets.svelte+32-31