mishig HF staff commited on
Commit
635c529
1 Parent(s): e08d702

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: "user", content: "Tell me a story" }];
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
- code: `import { HfInference } from '@huggingface/inference'
107
-
108
- const hf = new HfInference('your access token')`
109
  });
110
  if (conversation.config.streaming) {
111
  snippets.push({
112
  label: 'Streaming API',
113
- code: `let out = "";
114
 
115
- for await (const chunk of hf.chatCompletionStream({
116
- model: "${conversation.model}",
117
- messages: [
118
- { role: "user", content: "Complete the equation 1+1= ,just the answer" },
119
- ],
120
- max_tokens: ${conversation.config.maxTokens},
121
- temperature: ${conversation.config.temperature},
122
- seed: 0,
123
- })) {
124
- if (chunk.choices && chunk.choices.length > 0) {
125
- out += chunk.choices[0].delta.content;
126
- }
127
- }`
128
  });
129
  } else {
130
  // non-streaming
131
  snippets.push({
132
  label: 'Non-Streaming API',
133
- code: `await hf.chatCompletion({
134
- model: "${conversation.model}",
135
- messages: [
136
- { role: "user", content: "Complete the this sentence with words one plus one is equal " }
137
- ],
138
- max_tokens: ${conversation.config.maxTokens},
139
- temperature: ${conversation.config.temperature},
140
- seed: 0,
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