Support python snippets (#20)
Browse files
src/lib/components/CodeSnippets.svelte
CHANGED
@@ -33,8 +33,8 @@
|
|
33 |
|
34 |
let selectedLanguage: Language = 'javascript';
|
35 |
|
36 |
-
function getMessages(){
|
37 |
-
const placeholder = [{ role:
|
38 |
const messages = conversation.messages.length ? conversation.messages : placeholder;
|
39 |
return JSON.stringify(messages, null, 2);
|
40 |
}
|
@@ -65,8 +65,8 @@ let out = "";
|
|
65 |
for await (const chunk of inference.chatCompletionStream({
|
66 |
model: "${conversation.model}",
|
67 |
messages: ${messagesStr},
|
68 |
-
max_tokens: ${conversation.config.maxTokens},
|
69 |
temperature: ${conversation.config.temperature},
|
|
|
70 |
seed: 0,
|
71 |
})) {
|
72 |
if (chunk.choices && chunk.choices.length > 0) {
|
@@ -87,8 +87,8 @@ const inference = new HfInference("your access token")
|
|
87 |
const out = await inference.chatCompletion({
|
88 |
model: "${conversation.model}",
|
89 |
messages: ${messagesStr},
|
90 |
-
max_tokens: ${conversation.config.maxTokens},
|
91 |
temperature: ${conversation.config.temperature},
|
|
|
92 |
seed: 0,
|
93 |
});
|
94 |
|
@@ -100,45 +100,46 @@ console.log(out.choices[0].message);`
|
|
100 |
}
|
101 |
|
102 |
function getPythonSnippets() {
|
|
|
103 |
const snippets: Snippet[] = [];
|
104 |
snippets.push({
|
105 |
-
label: 'Install',
|
106 |
-
|
107 |
-
|
108 |
-
const hf = new HfInference('your access token')`
|
109 |
});
|
110 |
if (conversation.config.streaming) {
|
111 |
snippets.push({
|
112 |
label: 'Streaming API',
|
113 |
-
code: `
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
}
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
}`
|
128 |
});
|
129 |
} else {
|
130 |
// non-streaming
|
131 |
snippets.push({
|
132 |
label: 'Non-Streaming API',
|
133 |
-
code: `
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
})
|
|
|
|
|
142 |
});
|
143 |
}
|
144 |
|
|
|
33 |
|
34 |
let selectedLanguage: Language = 'javascript';
|
35 |
|
36 |
+
function getMessages() {
|
37 |
+
const placeholder = [{ role: 'user', content: 'Tell me a story' }];
|
38 |
const messages = conversation.messages.length ? conversation.messages : placeholder;
|
39 |
return JSON.stringify(messages, null, 2);
|
40 |
}
|
|
|
65 |
for await (const chunk of inference.chatCompletionStream({
|
66 |
model: "${conversation.model}",
|
67 |
messages: ${messagesStr},
|
|
|
68 |
temperature: ${conversation.config.temperature},
|
69 |
+
max_tokens: ${conversation.config.maxTokens},
|
70 |
seed: 0,
|
71 |
})) {
|
72 |
if (chunk.choices && chunk.choices.length > 0) {
|
|
|
87 |
const out = await inference.chatCompletion({
|
88 |
model: "${conversation.model}",
|
89 |
messages: ${messagesStr},
|
|
|
90 |
temperature: ${conversation.config.temperature},
|
91 |
+
max_tokens: ${conversation.config.maxTokens},
|
92 |
seed: 0,
|
93 |
});
|
94 |
|
|
|
100 |
}
|
101 |
|
102 |
function getPythonSnippets() {
|
103 |
+
const messagesStr = getMessages();
|
104 |
const snippets: Snippet[] = [];
|
105 |
snippets.push({
|
106 |
+
label: 'Install huggingface_hub',
|
107 |
+
language: 'bash',
|
108 |
+
code: `pip install huggingface_hub`
|
|
|
109 |
});
|
110 |
if (conversation.config.streaming) {
|
111 |
snippets.push({
|
112 |
label: 'Streaming API',
|
113 |
+
code: `from huggingface_hub import InferenceClient
|
114 |
|
115 |
+
model_id="${conversation.model}"
|
116 |
+
hf_token = "your HF token"
|
117 |
+
inference_client = InferenceClient(model_id, token=hf_token)
|
118 |
+
|
119 |
+
output = ""
|
120 |
+
|
121 |
+
messages = ${messagesStr}
|
122 |
+
|
123 |
+
for token in inference_client.chat_completion(messages, stream=True, temperature=${conversation.config.temperature}, max_tokens=${conversation.config.maxTokens}):
|
124 |
+
new_content = token.choices[0].delta.content
|
125 |
+
print(new_content, end="")
|
126 |
+
output += new_content`
|
|
|
127 |
});
|
128 |
} else {
|
129 |
// non-streaming
|
130 |
snippets.push({
|
131 |
label: 'Non-Streaming API',
|
132 |
+
code: `from huggingface_hub import InferenceClient
|
133 |
+
|
134 |
+
model_id="${conversation.model}"
|
135 |
+
hf_token = "your HF token"
|
136 |
+
inference_client = InferenceClient(model_id, token=hf_token)
|
137 |
+
|
138 |
+
messages = ${messagesStr}
|
139 |
+
|
140 |
+
output = inference_client.chat_completion(messages, temperature=${conversation.config.temperature}, max_tokens=${conversation.config.maxTokens})
|
141 |
+
|
142 |
+
print(output.choices[0].message)`
|
143 |
});
|
144 |
}
|
145 |
|