From 7527534fe61d8e4901758b3aec323289f1f4d0fd Mon Sep 17 00:00:00 2001 From: DavdGao Date: Wed, 15 May 2024 21:03:04 +0800 Subject: [PATCH] fix format error in tutorial (#226) --- .../en/source/tutorial/203-parser.md | 146 ++++++++---------- .../zh_CN/source/tutorial/203-parser.md | 146 ++++++++---------- 2 files changed, 126 insertions(+), 166 deletions(-) diff --git a/docs/sphinx_doc/en/source/tutorial/203-parser.md b/docs/sphinx_doc/en/source/tutorial/203-parser.md index 1ce87be1a..a4e0538c3 100644 --- a/docs/sphinx_doc/en/source/tutorial/203-parser.md +++ b/docs/sphinx_doc/en/source/tutorial/203-parser.md @@ -51,13 +51,12 @@ The main functions of the parser module include: 1. Provide "format instruction", that is, remind LLM where to generate what output, for example -> You should generate python code in a fenced code block as follows -> -> \```python -> -> {your_python_code} -> -> \``` +```` +You should generate python code in a fenced code block as follows +```python +{your_python_code} +``` +```` 2. Provide a parse function, which directly parses the text generated by LLM into the target data format, @@ -82,9 +81,7 @@ In the following sections, we will introduce the usage of these parsers based on ### String Type -
- - MarkdownCodeBlockParser +#### MarkdownCodeBlockParser ##### Initialization @@ -93,34 +90,32 @@ In the following sections, we will introduce the usage of these parsers based on ```python from agentscope.parsers import MarkdownCodeBlockParser - parser = MarkdownCodeBlockParser(language_name="python") + parser = MarkdownCodeBlockParser(language_name="python", content_hint="your python code") ``` ##### Format Instruction Template - `MarkdownCodeBlockParser` provides the following format instruction template. When the user calls the `format_instruction` attribute, `{language_name}` will be replaced with the string entered at initialization: - > You should generate {language_name} code in a {language_name} fenced code block as follows: - > - > \```{language_name} - > - > ${your_{language_name}_code} - > - > \``` + ```` + You should generate {language_name} code in a {language_name} fenced code block as follows: + ```{language_name} + {content_hint} + ``` + ```` - For the above initialization with `language_name` as `"python"`, when the `format_instruction` attribute is called, the following string will be returned: - ```python - print(parser.format_instruction) - ``` + ```python + print(parser.format_instruction) + ``` - > You should generate python code in a python fenced code block as follows - > - > \```python - > - > ${your_python_code} - > - > \``` + ```` + You should generate python code in a python fenced code block as follows + ```python + your python code + ``` + ```` ##### Parse Function @@ -140,9 +135,9 @@ In the following sections, we will introduce the usage of these parsers based on print(res.parsed) ```` - > print("hello world!") - -
+ ``` + print("hello world!") + ``` ### Dictionary Type @@ -259,18 +254,16 @@ In AgentScope, we achieve post-processing by calling the `to_content`, `to_memor > print(parser.to_memory(example_dict)) # {"thought": "abc", "speak": "def"} > print(parser.to_metadata(example_dict)) # None > ``` -> > def -> > -> > {"thought": "abc", "speak": "def"} -> > -> > None +> ``` +> def +> {"thought": "abc", "speak": "def"} +> None +> ``` Next we will introduce two parsers for dictionary type. -
- - MarkdownJsonDictParser +#### MarkdownJsonDictParser ##### Initialization & Format Instruction Template @@ -300,19 +293,14 @@ This parameter can be a string or a dictionary. For dictionary, it will be autom - The corresponding `instruction_format` attribute - > You should respond a json object in a json fenced code block as follows: - > - > \```json - > - > {content_hint} - > - > \``` - -
- -
+ ```` + You should respond a json object in a json fenced code block as follows: + ```json + {content_hint} + ``` + ```` - MultiTaggedContentParser +#### MultiTaggedContentParser `MultiTaggedContentParser` asks LLM to generate specific content within multiple tag pairs. The content from different tag pairs will be parsed into a single Python dictionary. Its usage is similar to `MarkdownJsonDictParser`, but the initialization method is different, and it is more suitable for weak LLMs or complex return content. @@ -352,13 +340,12 @@ parser = MultiTaggedContentParser( print(parser.format_instruction) ``` -> Respond with specific tags as outlined below, and the content between [FINISH_DISCUSSION] and [/FINISH_DISCUSSION] MUST be a JSON object: -> -> [THOUGHT]what you thought[/THOUGHT] -> -> [SPEAK]what you speak[/SPEAK] -> -> [FINISH_DISCUSSION]true/false, whether the discussion is finished[/FINISH_DISCUSSION] +``` +Respond with specific tags as outlined below, and the content between [FINISH_DISCUSSION] and [/FINISH_DISCUSSION] MUST be a JSON object: +[THOUGHT]what you thought[/THOUGHT] +[SPEAK]what you speak[/SPEAK] +[FINISH_DISCUSSION]true/false, whether the discussion is finished[/FINISH_DISCUSSION] +``` ##### Parse Function @@ -378,23 +365,17 @@ res_dict = parser.parse( print(res_dict) ``` -> { -> -> "thought": "The others didn't realize I was a werewolf. I should end the discussion soon.", -> -> "speak": "I agree with you.", -> -> "finish_discussion": true -> -> } - -
+``` +{ + "thought": "The others didn't realize I was a werewolf. I should end the discussion soon.", + "speak": "I agree with you.", + "finish_discussion": true +} +``` ### JSON / Python Object Type -
- - MarkdownJsonObjectParser +#### MarkdownJsonObjectParser `MarkdownJsonObjectParser` also uses the \```json and \``` tags in Markdown, but does not limit the content type. It can be a list, dictionary, number, string, etc., which can be parsed into a Python object via `json.loads`. @@ -410,13 +391,12 @@ parser = MarkdownJsonObjectParser( print(parser.format_instruction) ``` -> You should respond a json object in a json fenced code block as follows: -> -> \```json -> -> {a list of numbers} -> -> \``` +```` +You should respond a json object in a json fenced code block as follows: +```json +{a list of numbers} +``` +```` ##### Parse Function @@ -433,11 +413,11 @@ res = parser.parse( print(type(res)) print(res) ```` -> -> -> [1, 2, 3, 4, 5] -
+``` + +[1, 2, 3, 4, 5] +``` ## Typical Use Cases diff --git a/docs/sphinx_doc/zh_CN/source/tutorial/203-parser.md b/docs/sphinx_doc/zh_CN/source/tutorial/203-parser.md index a8b2a30fb..527f2960e 100644 --- a/docs/sphinx_doc/zh_CN/source/tutorial/203-parser.md +++ b/docs/sphinx_doc/zh_CN/source/tutorial/203-parser.md @@ -52,13 +52,12 @@ AgentScope中,解析器模块的设计原则是: 1. 提供“响应格式说明”(format instruction),即提示 LLM 应该在什么位置产生什么输出,例如 -> You should generate python code in a fenced code block as follows -> -> \```python -> -> {your_python_code} -> -> \``` +```` +You should generate python code in a fenced code block as follows +```python +{your_python_code} +``` +```` 2. 提供解析函数(parse function),直接将 LLM 产生的文本解析成目标数据格式 @@ -80,9 +79,7 @@ AgentScope提供了多种不同解析器,开发者可以根据自己的需求 ### 字符串(`str`)类型 -
- - MarkdownCodeBlockParser +#### MarkdownCodeBlockParser ##### 初始化 @@ -91,34 +88,32 @@ AgentScope提供了多种不同解析器,开发者可以根据自己的需求 ```python from agentscope.parsers import MarkdownCodeBlockParser - parser = MarkdownCodeBlockParser(language_name="python") + parser = MarkdownCodeBlockParser(language_name="python", content_hint="your python code") ``` ##### 响应格式模版 - `MarkdownCodeBlockParser`类提供如下的“响应格式说明”模版,在用户调用`format_instruction`属性时,会将`{language_name}`替换为初始化时输入的字符串: - > You should generate {language_name} code in a {language_name} fenced code block as follows: - > - > \```{language_name} - > - > ${your_{language_name}_code} - > - > \``` + ```` + You should generate {language_name} code in a {language_name} fenced code block as follows: + ```{language_name} + {content_hint} + ``` + ```` - 例如上述对`language_name`为`"python"`的初始化,调用`format_instruction`属性时,会返回如下字符串: - ```python - print(parser.format_instruction) - ``` + ```python + print(parser.format_instruction) + ``` - > You should generate python code in a python fenced code block as follows - > - > \```python - > - > ${your_python_code} - > - > \``` + ```` + You should generate python code in a python fenced code block as follows + ```python + your python code + ``` + ```` ##### 解析函数 @@ -138,9 +133,9 @@ AgentScope提供了多种不同解析器,开发者可以根据自己的需求 print(res.parsed) ```` - > print("hello world!") - -
+ ``` + print("hello world!") + ``` ### 字典(`dict`)类型 @@ -260,17 +255,15 @@ AgentScope中,我们通过调用`to_content`,`to_memory`和`to_metadata`方 > print(parser.to_memory(example_dict)) # {"thought": "abc", "speak": "def"} > print(parser.to_metadata(example_dict)) # None > ``` -> > def -> > -> > {"thought": "abc", "speak": "def"} -> > -> > None +> ``` +> def +> {"thought": "abc", "speak": "def"} +> None +> ``` 下面我们具体介绍两种字典类型的解析器。 -
- - MarkdownJsonDictParser +#### MarkdownJsonDictParser ##### 初始化 & 响应格式模版 @@ -297,19 +290,14 @@ AgentScope中,我们通过调用`to_content`,`to_memory`和`to_metadata`方 ``` - 对应的`instruction_format`属性 - > You should respond a json object in a json fenced code block as follows: - > - > \```json - > - > {content_hint} - > - > \``` - -
- -
+ ```` + You should respond a json object in a json fenced code block as follows: + ```json + {content_hint} + ``` + ```` - MultiTaggedContentParser +#### MultiTaggedContentParser `MultiTaggedContentParser`要求 LLM 在多个指定的标签对中产生指定的内容,这些不同标签的内容将一同被解析为一个 Python 字典。使用方法与`MarkdownJsonDictParser`类似,只是初始化方法不同,更适合能力较弱的LLM,或是比较复杂的返回内容。 @@ -349,13 +337,12 @@ parser = MultiTaggedContentParser( print(parser.format_instruction) ``` -> Respond with specific tags as outlined below, and the content between [FINISH_DISCUSSION] and [/FINISH_DISCUSSION] MUST be a JSON object: -> -> [THOUGHT]what you thought[/THOUGHT] -> -> [SPEAK]what you speak[/SPEAK] -> -> [FINISH_DISCUSSION]true/false, whether the discussion is finished[/FINISH_DISCUSSION] +``` +Respond with specific tags as outlined below, and the content between [FINISH_DISCUSSION] and [/FINISH_DISCUSSION] MUST be a JSON object: +[THOUGHT]what you thought[/THOUGHT] +[SPEAK]what you speak[/SPEAK] +[FINISH_DISCUSSION]true/false, whether the discussion is finished[/FINISH_DISCUSSION] +``` ##### 解析函数 @@ -374,23 +361,17 @@ res_dict = parser.parse( print(res_dict) ``` -> { -> -> "thought": "The others didn't realize I was a werewolf. I should end the discussion soon.", -> -> "speak": "I agree with you.", -> -> "finish_discussion": true -> -> } - -
+``` +{ + "thought": "The others didn't realize I was a werewolf. I should end the discussion soon.", + "speak": "I agree with you.", + "finish_discussion": true +} +``` ### JSON / Python 对象类型 -
- - MarkdownJsonObjectParser +#### MarkdownJsonObjectParser `MarkdownJsonObjectParser`同样采用 Markdown 的\```json和\```标识,但是不限制解析的内容的类型,可以是列表,字典,数值,字符串等可以通过`json.loads`进行解析字符串。 @@ -406,13 +387,12 @@ parser = MarkdownJsonObjectParser( print(parser.format_instruction) ``` -> You should respond a json object in a json fenced code block as follows: -> -> \```json -> -> {a list of numbers} -> -> \``` +```` +You should respond a json object in a json fenced code block as follows: +```json +{a list of numbers} +``` +```` ##### 解析函数 @@ -429,11 +409,11 @@ res = parser.parse( print(type(res)) print(res) ```` -> -> -> [1, 2, 3, 4, 5] -
+``` + +[1, 2, 3, 4, 5] +``` ## 典型使用样例