|
1 | 1 | # ---------------------------------------------------------
|
2 | 2 | # Copyright (c) Microsoft Corporation. All rights reserved.
|
3 | 3 | # ---------------------------------------------------------
|
| 4 | +# pylint: disable=C0303 |
| 5 | +""" |
| 6 | +This module contains a utility class for managing a list of JSON lines. |
| 7 | +""" |
4 | 8 | import json
|
5 | 9 |
|
6 | 10 |
|
7 | 11 | class JsonLineList(list):
|
| 12 | + """ |
| 13 | + A util to manage a list of JSON lines. |
| 14 | + """ |
8 | 15 | def to_json_lines(self):
|
| 16 | + """ |
| 17 | + Converts the list to a string of JSON lines. |
| 18 | + Each item in the list is converted to a JSON string |
| 19 | + and appended to the result string with a newline. |
| 20 | + |
| 21 | + :returns: A string of JSON lines, where each line is a JSON representation of an item in the list. |
| 22 | + :rtype: str |
| 23 | + """ |
9 | 24 | json_lines = ""
|
10 | 25 | for item in self:
|
11 | 26 | json_lines += json.dumps(item) + "\n"
|
12 | 27 | return json_lines
|
13 | 28 |
|
14 | 29 | def to_eval_qa_json_lines(self):
|
| 30 | + """ |
| 31 | + Converts the list to a string of JSON lines suitable for evaluation in a Q&A format. |
| 32 | + Each item in the list is expected to be a dictionary with |
| 33 | + 'messages' key. The 'messages' value is a list of |
| 34 | + dictionaries, each with a 'role' key and a 'content' key. |
| 35 | + The 'role' value should be either 'user' or 'assistant', |
| 36 | + and the 'content' value should be a string. |
| 37 | + If a 'context' key is present in the message, its value is also included |
| 38 | + in the output. |
| 39 | + |
| 40 | + :returns: A string of JSON lines. |
| 41 | + :rtype: str |
| 42 | + """ |
15 | 43 | json_lines = ""
|
16 | 44 | for item in self:
|
17 | 45 | user_message = None
|
18 | 46 | assistant_message = None
|
19 |
| - for message in item["messages"]: |
20 |
| - if message["role"] == "user": |
21 |
| - user_message = message["content"] |
22 |
| - elif message["role"] == "assistant": |
23 |
| - assistant_message = message["content"] |
| 47 | + context = None |
| 48 | + for message in item['messages']: |
| 49 | + if message['role'] == 'user': |
| 50 | + user_message = message['content'] |
| 51 | + elif message['role'] == 'assistant': |
| 52 | + assistant_message = message['content'] |
| 53 | + if 'context' in message: |
| 54 | + context = message.get("context", None) |
24 | 55 | if user_message and assistant_message:
|
25 |
| - json_lines += json.dumps({"question": user_message, "answer": assistant_message}) + "\n" |
| 56 | + if context: |
| 57 | + json_lines += json.dumps({ |
| 58 | + 'question': user_message, |
| 59 | + 'answer': assistant_message, |
| 60 | + 'context': context}) + "\n" |
| 61 | + else: |
| 62 | + json_lines += json.dumps({ |
| 63 | + 'question': user_message, |
| 64 | + 'answer': assistant_message}) + "\n" |
26 | 65 | return json_lines
|
0 commit comments