RangiLyu commited on
Commit
ff0c7fd
·
verified ·
1 Parent(s): dad998b

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +400 -0
README.md CHANGED
@@ -1,3 +1,403 @@
1
  ---
2
  license: apache-2.0
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
+ pipeline_tag: image-text-to-text
4
  ---
5
+
6
+
7
+ ## Intern-S1
8
+
9
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/642695e5274e7ad464c8a5ba/E43cgEXBRWjVJlU_-hdh6.png)
10
+
11
+ ## Introduction
12
+
13
+ We introduce **Intern-S1**, our **most advanced open-source multimodal reasoning model** to date. Intern-S1 combines **strong general-task capabilities with state-of-the-art performance on a wide range of scientific tasks**, rivaling leading closed-source commercial models.
14
+ Built upon a 235B MoE language model and a 6B Vision encoder, Intern-S1 has been further pretrained on **5 trillion tokens** of multimodal data, including over **2.5 trillion scientific-domain tokens**. This enables the model to retain strong general capabilities while excelling in specialized scientific domains such as **interpreting chemical structures, understanding protein sequences, and planning compound synthesis routes**, making Intern-S1 to be a capable research assistant for real-world scientific applications.
15
+ Features
16
+
17
+ - Strong performance across language and vision reasoning benchmarks, especially scientific tasks.
18
+
19
+ - Continuously pretrained on a massive 5T token dataset, with over 50% specialized scientific data, embedding deep domain expertise.
20
+
21
+ - Dynamic tokenizer enables native understanding of molecular formulas, protein sequences, and seismic signals.
22
+
23
+ ## Performance
24
+
25
+ We evaluate the Intern-S1 on various benchmarks including general datasets and scientifc datasets. We report the performance comparsion with the recent VLMs and LLMs below.
26
+
27
+
28
+
29
+ We use the [OpenCompass](https://github.com/open-compass/OpenCompass/) and [VLMEvalkit](https://github.com/open-compass/vlmevalkit) to evaluate all models.
30
+
31
+
32
+ ## Quick Start
33
+
34
+ ### Sampling Parameters
35
+
36
+ We recommend using the following hyperparameters to ensure better results
37
+
38
+ ```python
39
+ top_p = 1.0
40
+ top_k = 50
41
+ min_p = 0.0
42
+ temperature = 0.7
43
+ ```
44
+
45
+ ### Transformers
46
+
47
+ The following provides demo code illustrating how to generate based on text and multimodal inputs.
48
+
49
+ > **Please use transformers>=4.53.0 to ensure the model works normally.**
50
+
51
+ #### Text input
52
+
53
+ ```python
54
+ from transformers import AutoProcessor, AutoModelForCausalLM
55
+ import torch
56
+
57
+ model_name = "internlm/Intern-S1"
58
+ processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
59
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
60
+
61
+ messages = [
62
+ {
63
+ "role": "user",
64
+ "content": [
65
+ {"type": "text", "text": "tell me about an interesting physical phenomenon."},
66
+ ],
67
+ }
68
+ ]
69
+
70
+ inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device, dtype=torch.bfloat16)
71
+
72
+ generate_ids = model.generate(**inputs, max_new_tokens=32768)
73
+ decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
74
+ print(decoded_output)
75
+ ```
76
+
77
+ #### Image input
78
+
79
+ ```python
80
+ from transformers import AutoProcessor, AutoModelForCausalLM
81
+ import torch
82
+
83
+ model_name = "internlm/Intern-S1"
84
+ processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
85
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
86
+
87
+ messages = [
88
+ {
89
+ "role": "user",
90
+ "content": [
91
+ {"type": "image", "url": "http://images.cocodataset.org/val2017/000000039769.jpg"},
92
+ {"type": "text", "text": "Please describe the image explicitly."},
93
+ ],
94
+ }
95
+ ]
96
+
97
+ inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device, dtype=torch.bfloat16)
98
+
99
+ generate_ids = model.generate(**inputs, max_new_tokens=32768)
100
+ decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
101
+ print(decoded_output)
102
+ ```
103
+
104
+ #### Video input
105
+
106
+ Please ensure that the decord video decoding library is installed via `pip install decord`.
107
+
108
+ ```python
109
+ from transformers import AutoProcessor, AutoModelForCausalLM
110
+ import torch
111
+
112
+ model_name = "internlm/Intern-S1"
113
+ processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
114
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
115
+
116
+ messages = [
117
+ {
118
+ "role": "user",
119
+ "content": [
120
+ {
121
+ "type": "video",
122
+ "url": "https://huggingface.co/datasets/hf-internal-testing/fixtures_videos/resolve/main/tennis.mp4",
123
+ },
124
+ {"type": "text", "text": "What type of shot is the man performing?"},
125
+ ],
126
+ }
127
+ ]
128
+
129
+ inputs = processor.apply_chat_template(
130
+ messages,
131
+ return_tensors="pt",
132
+ add_generation_prompt=True,
133
+ video_load_backend="decord",
134
+ tokenize=True,
135
+ return_dict=True,
136
+ ).to(model.device, dtype=torch.float16)
137
+
138
+ generate_ids = model.generate(**inputs, max_new_tokens=32768)
139
+ decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
140
+ print(decoded_output)
141
+ ```
142
+
143
+ ### Serving
144
+
145
+ You can utilize one of the following LLM inference frameworks to create an OpenAI compatible server:
146
+
147
+ #### [lmdeploy(>=0.9.2)](https://github.com/InternLM/lmdeploy)
148
+
149
+ ```
150
+ lmdeploy serve api_server internlm/Intern-S1 --reasoning-parser intern-s1 --tool-call-parser intern-s1 --tp 8
151
+ ```
152
+
153
+ #### [vllm](https://github.com/vllm-project/vllm)
154
+
155
+ Coming soon.
156
+
157
+ #### [sglang](https://github.com/sgl-project/sglang)
158
+
159
+ ```bash
160
+ CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 \
161
+ python3 -m sglang.launch_server \
162
+ --model-path internlm/Intern-S1 \
163
+ --trust-remote-code \
164
+ --tp 8 \
165
+ --enable-multimodal \
166
+ --grammar-backend none
167
+ ```
168
+
169
+ #### ollama for local deployment:
170
+
171
+ ```bash
172
+ # install ollama
173
+ curl -fsSL https://ollama.com/install.sh | sh
174
+ # fetch model
175
+ ollama pull internlm/Intern-S1
176
+ # run model
177
+ ollama run internlm/Intern-S1
178
+ # then use openai client to call on http://localhost:11434/v1
179
+ ```
180
+
181
+ ## Advanced Usage
182
+
183
+ ### Tool Calling
184
+
185
+ Many Large Language Models (LLMs) now feature **Tool Calling**, a powerful capability that allows them to extend their functionality by interacting with external tools and APIs. This enables models to perform tasks like fetching up-to-the-minute information, running code, or calling functions within other applications.
186
+
187
+ A key advantage for developers is that a growing number of open-source LLMs are designed to be compatible with the OpenAI API. This means you can leverage the same familiar syntax and structure from the OpenAI library to implement tool calling with these open-source models. As a result, the code demonstrated in this tutorial is versatile—it works not just with OpenAI models, but with any model that follows the same interface standard.
188
+
189
+ To illustrate how this works, let's dive into a practical code example that uses tool calling to get the latest weather forecast.
190
+
191
+ ```python
192
+ from openai import OpenAI
193
+ import json
194
+
195
+
196
+ def get_current_temperature(location: str, unit: str = "celsius"):
197
+ """Get current temperature at a location.
198
+
199
+ Args:
200
+ location: The location to get the temperature for, in the format "City, State, Country".
201
+ unit: The unit to return the temperature in. Defaults to "celsius". (choices: ["celsius", "fahrenheit"])
202
+
203
+ Returns:
204
+ the temperature, the location, and the unit in a dict
205
+ """
206
+ return {
207
+ "temperature": 26.1,
208
+ "location": location,
209
+ "unit": unit,
210
+ }
211
+
212
+
213
+ def get_temperature_date(location: str, date: str, unit: str = "celsius"):
214
+ """Get temperature at a location and date.
215
+
216
+ Args:
217
+ location: The location to get the temperature for, in the format "City, State, Country".
218
+ date: The date to get the temperature for, in the format "Year-Month-Day".
219
+ unit: The unit to return the temperature in. Defaults to "celsius". (choices: ["celsius", "fahrenheit"])
220
+
221
+ Returns:
222
+ the temperature, the location, the date and the unit in a dict
223
+ """
224
+ return {
225
+ "temperature": 25.9,
226
+ "location": location,
227
+ "date": date,
228
+ "unit": unit,
229
+ }
230
+
231
+ def get_function_by_name(name):
232
+ if name == "get_current_temperature":
233
+ return get_current_temperature
234
+ if name == "get_temperature_date":
235
+ return get_temperature_date
236
+
237
+ tools = [{
238
+ 'type': 'function',
239
+ 'function': {
240
+ 'name': 'get_current_temperature',
241
+ 'description': 'Get current temperature at a location.',
242
+ 'parameters': {
243
+ 'type': 'object',
244
+ 'properties': {
245
+ 'location': {
246
+ 'type': 'string',
247
+ 'description': 'The location to get the temperature for, in the format \'City, State, Country\'.'
248
+ },
249
+ 'unit': {
250
+ 'type': 'string',
251
+ 'enum': [
252
+ 'celsius',
253
+ 'fahrenheit'
254
+ ],
255
+ 'description': 'The unit to return the temperature in. Defaults to \'celsius\'.'
256
+ }
257
+ },
258
+ 'required': [
259
+ 'location'
260
+ ]
261
+ }
262
+ }
263
+ }, {
264
+ 'type': 'function',
265
+ 'function': {
266
+ 'name': 'get_temperature_date',
267
+ 'description': 'Get temperature at a location and date.',
268
+ 'parameters': {
269
+ 'type': 'object',
270
+ 'properties': {
271
+ 'location': {
272
+ 'type': 'string',
273
+ 'description': 'The location to get the temperature for, in the format \'City, State, Country\'.'
274
+ },
275
+ 'date': {
276
+ 'type': 'string',
277
+ 'description': 'The date to get the temperature for, in the format \'Year-Month-Day\'.'
278
+ },
279
+ 'unit': {
280
+ 'type': 'string',
281
+ 'enum': [
282
+ 'celsius',
283
+ 'fahrenheit'
284
+ ],
285
+ 'description': 'The unit to return the temperature in. Defaults to \'celsius\'.'
286
+ }
287
+ },
288
+ 'required': [
289
+ 'location',
290
+ 'date'
291
+ ]
292
+ }
293
+ }
294
+ }]
295
+
296
+
297
+
298
+ messages = [
299
+ {'role': 'user', 'content': 'Today is 2024-11-14, What\'s the temperature in San Francisco now? How about tomorrow?'}
300
+ ]
301
+
302
+ openai_api_key = "EMPTY"
303
+ openai_api_base = "http://0.0.0.0:23333/v1"
304
+ client = OpenAI(
305
+ api_key=openai_api_key,
306
+ base_url=openai_api_base,
307
+ )
308
+ model_name = client.models.list().data[0].id
309
+ response = client.chat.completions.create(
310
+ model=model_name,
311
+ messages=messages,
312
+ max_tokens=32768,
313
+ temperature=0.8,
314
+ top_p=0.8,
315
+ stream=False,
316
+ extra_body=dict(spaces_between_special_tokens=False),
317
+ tools=tools)
318
+ print(response.choices[0].message)
319
+ messages.append(response.choices[0].message)
320
+
321
+ for tool_call in response.choices[0].message.tool_calls:
322
+ tool_call_args = json.loads(tool_call.function.arguments)
323
+ tool_call_result = get_function_by_name(tool_call.function.name)(**tool_call_args)
324
+ tool_call_result = json.dumps(tool_call_result, ensure_ascii=False)
325
+ messages.append({
326
+ 'role': 'tool',
327
+ 'name': tool_call.function.name,
328
+ 'content': tool_call_result,
329
+ 'tool_call_id': tool_call.id
330
+ })
331
+
332
+ response = client.chat.completions.create(
333
+ model=model_name,
334
+ messages=messages,
335
+ temperature=0.8,
336
+ top_p=0.8,
337
+ stream=False,
338
+ extra_body=dict(spaces_between_special_tokens=False),
339
+ tools=tools)
340
+ print(response.choices[0].message.content)
341
+ ```
342
+
343
+
344
+ ### Switching Between Thinking and Non-Thinking Modes
345
+
346
+ Intern-S1 enables thinking mode by default, enhancing the model's reasoning capabilities to generate higher-quality responses. This feature can be disabled by setting `enable_thinking=False` in `tokenizer.apply_chat_template`
347
+
348
+ ```python
349
+ text = tokenizer.apply_chat_template(
350
+ messages,
351
+ tokenize=False,
352
+ add_generation_prompt=True,
353
+ enable_thinking=False # think mode indicator
354
+ )
355
+ ```
356
+
357
+ With LMDeploy serving Intern-S1 models, you can dynamically control the thinking mode by adjusting the `enable_thinking` parameter in your requests.
358
+
359
+ ```python
360
+ from openai import OpenAI
361
+ import json
362
+
363
+ messages = [
364
+ {
365
+ 'role': 'user',
366
+ 'content': 'who are you'
367
+ }, {
368
+ 'role': 'assistant',
369
+ 'content': 'I am an AI'
370
+ }, {
371
+ 'role': 'user',
372
+ 'content': 'AGI is?'
373
+ }]
374
+
375
+ openai_api_key = "EMPTY"
376
+ openai_api_base = "http://0.0.0.0:23333/v1"
377
+ client = OpenAI(
378
+ api_key=openai_api_key,
379
+ base_url=openai_api_base,
380
+ )
381
+ model_name = client.models.list().data[0].id
382
+
383
+ response = client.chat.completions.create(
384
+ model=model_name,
385
+ messages=messages,
386
+ tools=tools,
387
+ temperature=0.7,
388
+ top_p=0.8,
389
+ max_tokens=2048,
390
+ extra_body={
391
+ "enable_thinking": False,
392
+ }
393
+ )
394
+ print(json.dumps(response.model_dump(), indent=2, ensure_ascii=False))
395
+ ```
396
+
397
+ For vllm and sglang users, configure this through,
398
+
399
+ ```python
400
+ extra_body={
401
+ "chat_template_kwargs": {"enable_thinking": false}
402
+ }
403
+ ```