JSON格式

JSON Formatter

如果你想讓語言模型的回答為JSON格式,可以使用JSONformatter

範例

如以下範例,首先先定義你想要回傳的key值的名稱、說明和型別,並利用JSON_formatter轉換成json格式的prompt,並在問問題時把JSON_prompt丟入system_prompt中。

如formatter2和formatter3,你也可以直接使用JSON_formatter_list或JSON_formatter_dict將list或dictionary轉換成json格式的prompt。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import akasha.prompts as prompts
import akasha
import akasha.eval as eval

formatter = [
prompts.OutputSchema(name="學歷", description="受試者的就讀大學", type="str"),
prompts.OutputSchema(name="經驗", description="受試者的工作經驗", type="str"),
prompts.OutputSchema(name="專長", description="受試者的專長能力", type="list"),
prompts.OutputSchema(name="年資", description="受試者的總工作年數", type="int")
]

formatter2 = prompts.JSON_formatter_list(names=["學歷","經驗","專長","年資"], types=["str","str","list","int"],\
descriptions=["受試者的就讀大學","受試者的工作經驗","受試者的專長能力","受試者的總工作年數"])

formatter3 = prompts.JSON_formatter_dict([{ "name": "學歷", "description": "受試者的就讀大學", "type": "str" },\
{ "name": "經驗", "description": "受試者的工作經驗", "type": "str" },\
{ "name": "專長", "description": "受試者的專長能力", "type": "list" },\
{ "name": "年資", "description": "受試者的總工作年數", "type": "int" }])


JSON_prompt = prompts.JSON_formatter(formatter)
# JSON_prompt = prompts.JSON_formatter(formatter2)
# JSON_prompt = prompts.JSON_formatter(formatter3)


ak = akasha.Doc_QA()

response = ak.ask_whole_file(file_path="docs/resume_pool/A.docx",
system_prompt=JSON_prompt, prompt=f'''以上是受試者的履歷,請回答該受試者的學歷、經驗、專長、年資''')

parse_json = akasha.helper.extract_json(resposne)

print(parse_json)
print(type(parse_json))
1
2
3
{'學歷': '國立臺北科技大學電資學士班 四技', '經驗': '總年資0-1年年工作經歷', '專長': ['計算機網路(協定)', '資料庫系統', '物件導向程式設計', 'C語言', 'python', 'C++', 'Gitlab', 'Jenkins', 'Git', 'linux', 'Google表單'], '年資': 1} 

<class 'dict'>