Skip to content

Markdown2json

LLMMarkdown2Json

Source code in Docs2KG/modules/llm/markdown2json.py
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
class LLMMarkdown2Json:
    def __init__(self, markdown_file: Path, llm_model_name: str = "gpt-3.5-turbo-0125"):
        """
        Convert markdown to json using OpenAI LLM

        There are two types of JSON (Structured format)

        - JSON for layout structure
        - JSON for content structure

        It will be interesting to compare and see the difference between the two

        Args:
            markdown_file (Path): The path to the markdown file
            llm_model_name (str): The OpenAI LLM model name
        """
        self.markdown_file = markdown_file
        if self.markdown_file.suffix != ".csv":
            raise ValueError("Only support csv")
        self.json_csv_file = markdown_file.with_suffix(".json.csv")
        self.llm_model_name = llm_model_name
        self.cost = 0

    def clean_markdown(self) -> Optional[str]:
        """
        Prompt will give the LLM Markdown text

        Ask it clean it, and then get the Markdown into proper format

        Returns:
            str: The cleaned Markdown text
        """
        current_cost = self.cost
        cleaned_markdown_csv = self.markdown_file.with_suffix(".cleaned.csv")
        if cleaned_markdown_csv.exists():
            logger.info(f"{cleaned_markdown_csv} already exists")
            return
        df = pd.read_csv(self.markdown_file)
        for index, row in tqdm(df.iterrows(), total=df.shape[0], desc="Layout JSON"):
            cleaned_markdown = self.openai_clean_markdown(row["text"])
            df.at[index, "text"] = cleaned_markdown
        df.to_csv(cleaned_markdown_csv, index=False)
        logger.info(f"Cost: {self.cost - current_cost}")

    def extract2json(self):
        if self.json_csv_file.exists():
            logger.info(f"{self.json_csv_file} already exists")
            return
        logger.info(self.markdown_file)
        current_cost = self.cost
        df = pd.read_csv(self.markdown_file)

        for index, row in tqdm(df.iterrows(), total=df.shape[0], desc="Layout JSON"):
            df.at[index, "layout_json"] = self.openai_layout_json(row["text"])
        for index, row in tqdm(df.iterrows(), total=df.shape[0], desc="Content JSON"):
            df.at[index, "content_json"] = self.openai_content_json(row["text"])

        df.to_csv(self.json_csv_file, index=False)
        logger.info(f"Cost: {self.cost - current_cost}")

    def openai_clean_markdown(self, markdown):
        """
        Use OpenAI LLM to clean the markdown

        The markdown will be cleaned using the OpenAI LLM

        Args:
            markdown (str): The Markdown text

        Returns:
            str: The cleaned Markdown text
        """
        try:
            messages = [
                {
                    "role": "system",
                    "content": """You are a helpful assistant to clean the markdown text.""",
                },
                {
                    "role": "user",
                    "content": f"""
                                You task is to clean the markdown.

                                Steps include

                                - Remove the content which is not meaningful, such as a single I character, etc
                                - Do not need to keep special characters, such as `#`, `*`, etc, only keep
                                    meaningful content
                                - Make sure the markdown is fit with the markdown format
                                - Only do remove for the noise characters, do not change any content

                                Output should be a cleaned markdown text, which in str format

                                It will stay in the response in json format
                                with a key "cleaned_markdown"

                                Clean the following markdown text:\n\n{markdown}
                                """,
                },
            ]
            markdown_json = self.llm_openai_call(messages)
            logger.debug(markdown_json)
            return json.loads(markdown_json).get("cleaned_markdown", None)
        except Exception as e:
            logger.exception(e)
            return None

    def openai_layout_json(self, markdown):
        """
        Use OpenAI LLM to convert markdown to json

        The markdown will be converted to json using the OpenAI LLM

        Output format should be like

        Examples:
            {
            "tag": "root",
            "content": {title},
            "children": [
                {
                "tag": "h1",
                "content": "This is the header 1",
                "children": [
                    {
                    "tag": "h2",
                    "content": "This is the header 2",
                    "children": [
                        ....
                        {
                        "tag": "p",
                        "content": "This is the paragraph",
                        "children": []
                        }
                    ]
                    }
                ]
                }
            ]
            }

        For Example:

        ```markdown
        # Title
        ## Subtitle
        ### Subtitle 2
        - Item 1
        - Item 2
        - Item 3

        ## Subtitle 3
        - Item 1
        - Item 2

        This is a paragraph

        ## Subtitle 4
        - Item 1
        - Item 2
        ```

        Should output as

        ```json
        {
        "tag": "h1",
        "content": "Title",
        "children": [
            {
                "tag": "h2",
                "content": "Subtitle",
                "children": [
                    {
                        "tag": "h3",
                        "content": "Subtitle 2",
                        "children": [
                            {
                                "tag": "li",
                                "content": "Item 1",
                                "children": []
                            },
                            {
                                "tag": "li",
                                "content": "Item 2",
                                "children": []
                            },
                            {
                                "tag": "li",
                                "content": "Item 3",
                                "children": []
                            }
                        ]
                    }
                ]
            },
            {
                "tag": "h2",
                "content": "Subtitle 3",
                "children": [
                    {
                        "tag": "li",
                        "content": "Item 1",
                        "children": []
                    },
                    {
                        "tag": "li",
                        "content": "Item 2",
                        "children": []
                    }
                ]
            },
            {
                "tag": "p",
                "content": "This is a paragraph",
                "children": []
            },
            {
                "tag": "h2",
                "content": "Subtitle 4",
                "children": [
                    {
                        "tag": "li",
                        "content": "Item 1",
                        "children": []
                    },
                    {
                        "tag": "li",
                        "content": "Item 2",
                        "children": []
                    }
                ]
            }
        ]
        }
        ```

        Args:
            markdown (str): The Markdown text

        Returns:

        """
        messages = [
            {
                "role": "system",
                "content": """You are a helpful assistant to convert markdown to JSON format.

        The markdown given to you will have some noise/not meaningful characters or information, you need to think
        about cleaning the markdown into a cleaned version.

        Then convert the markdown to json

        For Example:

        ```markdown
        # Title
        ## Subtitle
        ### Subtitle 2
        - Item 1
        - Item 2
        - Item 3

        ## Subtitle 3
        - Item 1
        - Item 2

        This is a paragraph

        ## Subtitle 4
        - Item 1
        - Item 2
        ```

        Should output as

        ```json
        {
        "tag": "h1",
        "content": "Title",
        "children": [
            {
                "tag": "h2",
                "content": "Subtitle",
                "children": [
                    {
                        "tag": "h3",
                        "content": "Subtitle 2",
                        "children": [
                            {
                                "tag": "li",
                                "content": "Item 1",
                                "children": []
                            },
                            {
                                "tag": "li",
                                "content": "Item 2",
                                "children": []
                            },
                            {
                                "tag": "li",
                                "content": "Item 3",
                                "children": []
                            }
                        ]
                    }
                ]
            },
            {
                "tag": "h2",
                "content": "Subtitle 3",
                "children": [
                    {
                        "tag": "li",
                        "content": "Item 1",
                        "children": []
                    },
                    {
                        "tag": "li",
                        "content": "Item 2",
                        "children": []
                    },
                    {
                        "tag": "p",
                        "content": "This is a paragraph",
                        "children": []
                    },
                ]
            },
            {
                "tag": "h2",
                "content": "Subtitle 4",
                "children": [
                    {
                        "tag": "li",
                        "content": "Item 1",
                        "children": []
                    },
                    {
                        "tag": "li",
                        "content": "Item 2",
                        "children": []
                    }
                ]
            }
        ]
        }
        ```

        tag will be from the html convention like h1, h2, h3, p, li, etc.

        Keep the meaningful hierarchy information within markdown via the html h1/h2/... tags
        Get them proper in json format.

        If it is a table, leave it as
        {
            "tag": "table",
            "content": "",
            "children": []
        }
        Content should the full content of the table, do not decompose further into tr/td/th, etc

        One example can be
        {
            "tag": "table",
            "content": ",header,header,
                        value,value....
                        ",
            "children": []
        }

        """,
            },
            {
                "role": "user",
                "content": f"Convert the following markdown to JSON format:\n\n{markdown}",
            },
        ]
        return self.llm_openai_call(messages)

    def openai_content_json(self, markdown: str):
        """
        Use OpenAI LLM to convert markdown to json

        The markdown will be converted to json using the OpenAI LLM
        Main focus here is to convert the content to json
        Args:
            markdown:

        Returns:

        """
        messages = [
            {
                "role": "system",
                "content": """
                    You are a helpful assistant to convert markdown to JSON format.
                    You will be focusing on extracting meaningful key-value pairs from the markdown text.
                    """,
            },
            {
                "role": "user",
                "content": f"Convert the following markdown to JSON format:\n\n{markdown}",
            },
        ]
        return self.llm_openai_call(messages)

    def llm_openai_call(self, messages: List[dict]) -> str:
        """
        Call the OpenAI API to get the response
        Args:
            messages (List[dict]): The messages to send to the OpenAI API


        Returns:
            response_json_str (str): The response from the OpenAI API
        """
        result_json_str, cost = openai_call(messages, self.llm_model_name)
        self.cost += cost
        return result_json_str

__init__(markdown_file, llm_model_name='gpt-3.5-turbo-0125')

Convert markdown to json using OpenAI LLM

There are two types of JSON (Structured format)

  • JSON for layout structure
  • JSON for content structure

It will be interesting to compare and see the difference between the two

Parameters:

Name Type Description Default
markdown_file Path

The path to the markdown file

required
llm_model_name str

The OpenAI LLM model name

'gpt-3.5-turbo-0125'
Source code in Docs2KG/modules/llm/markdown2json.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def __init__(self, markdown_file: Path, llm_model_name: str = "gpt-3.5-turbo-0125"):
    """
    Convert markdown to json using OpenAI LLM

    There are two types of JSON (Structured format)

    - JSON for layout structure
    - JSON for content structure

    It will be interesting to compare and see the difference between the two

    Args:
        markdown_file (Path): The path to the markdown file
        llm_model_name (str): The OpenAI LLM model name
    """
    self.markdown_file = markdown_file
    if self.markdown_file.suffix != ".csv":
        raise ValueError("Only support csv")
    self.json_csv_file = markdown_file.with_suffix(".json.csv")
    self.llm_model_name = llm_model_name
    self.cost = 0

clean_markdown()

Prompt will give the LLM Markdown text

Ask it clean it, and then get the Markdown into proper format

Returns:

Name Type Description
str Optional[str]

The cleaned Markdown text

Source code in Docs2KG/modules/llm/markdown2json.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def clean_markdown(self) -> Optional[str]:
    """
    Prompt will give the LLM Markdown text

    Ask it clean it, and then get the Markdown into proper format

    Returns:
        str: The cleaned Markdown text
    """
    current_cost = self.cost
    cleaned_markdown_csv = self.markdown_file.with_suffix(".cleaned.csv")
    if cleaned_markdown_csv.exists():
        logger.info(f"{cleaned_markdown_csv} already exists")
        return
    df = pd.read_csv(self.markdown_file)
    for index, row in tqdm(df.iterrows(), total=df.shape[0], desc="Layout JSON"):
        cleaned_markdown = self.openai_clean_markdown(row["text"])
        df.at[index, "text"] = cleaned_markdown
    df.to_csv(cleaned_markdown_csv, index=False)
    logger.info(f"Cost: {self.cost - current_cost}")

llm_openai_call(messages)

Call the OpenAI API to get the response Args: messages (List[dict]): The messages to send to the OpenAI API

Returns:

Name Type Description
response_json_str str

The response from the OpenAI API

Source code in Docs2KG/modules/llm/markdown2json.py
423
424
425
426
427
428
429
430
431
432
433
434
435
def llm_openai_call(self, messages: List[dict]) -> str:
    """
    Call the OpenAI API to get the response
    Args:
        messages (List[dict]): The messages to send to the OpenAI API


    Returns:
        response_json_str (str): The response from the OpenAI API
    """
    result_json_str, cost = openai_call(messages, self.llm_model_name)
    self.cost += cost
    return result_json_str

openai_clean_markdown(markdown)

Use OpenAI LLM to clean the markdown

The markdown will be cleaned using the OpenAI LLM

Parameters:

Name Type Description Default
markdown str

The Markdown text

required

Returns:

Name Type Description
str

The cleaned Markdown text

Source code in Docs2KG/modules/llm/markdown2json.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def openai_clean_markdown(self, markdown):
    """
    Use OpenAI LLM to clean the markdown

    The markdown will be cleaned using the OpenAI LLM

    Args:
        markdown (str): The Markdown text

    Returns:
        str: The cleaned Markdown text
    """
    try:
        messages = [
            {
                "role": "system",
                "content": """You are a helpful assistant to clean the markdown text.""",
            },
            {
                "role": "user",
                "content": f"""
                            You task is to clean the markdown.

                            Steps include

                            - Remove the content which is not meaningful, such as a single I character, etc
                            - Do not need to keep special characters, such as `#`, `*`, etc, only keep
                                meaningful content
                            - Make sure the markdown is fit with the markdown format
                            - Only do remove for the noise characters, do not change any content

                            Output should be a cleaned markdown text, which in str format

                            It will stay in the response in json format
                            with a key "cleaned_markdown"

                            Clean the following markdown text:\n\n{markdown}
                            """,
            },
        ]
        markdown_json = self.llm_openai_call(messages)
        logger.debug(markdown_json)
        return json.loads(markdown_json).get("cleaned_markdown", None)
    except Exception as e:
        logger.exception(e)
        return None

openai_content_json(markdown)

Use OpenAI LLM to convert markdown to json

The markdown will be converted to json using the OpenAI LLM Main focus here is to convert the content to json Args: markdown:

Returns:

Source code in Docs2KG/modules/llm/markdown2json.py
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
def openai_content_json(self, markdown: str):
    """
    Use OpenAI LLM to convert markdown to json

    The markdown will be converted to json using the OpenAI LLM
    Main focus here is to convert the content to json
    Args:
        markdown:

    Returns:

    """
    messages = [
        {
            "role": "system",
            "content": """
                You are a helpful assistant to convert markdown to JSON format.
                You will be focusing on extracting meaningful key-value pairs from the markdown text.
                """,
        },
        {
            "role": "user",
            "content": f"Convert the following markdown to JSON format:\n\n{markdown}",
        },
    ]
    return self.llm_openai_call(messages)

openai_layout_json(markdown)

Use OpenAI LLM to convert markdown to json

The markdown will be converted to json using the OpenAI LLM

Output format should be like

Examples:

{ "tag": "root", "content": {title}, "children": [ { "tag": "h1", "content": "This is the header 1", "children": [ { "tag": "h2", "content": "This is the header 2", "children": [ .... { "tag": "p", "content": "This is the paragraph", "children": [] } ] } ] } ] }

For Example:

# Title
## Subtitle
### Subtitle 2
- Item 1
- Item 2
- Item 3

## Subtitle 3
- Item 1
- Item 2

This is a paragraph

## Subtitle 4
- Item 1
- Item 2

Should output as

{
"tag": "h1",
"content": "Title",
"children": [
    {
        "tag": "h2",
        "content": "Subtitle",
        "children": [
            {
                "tag": "h3",
                "content": "Subtitle 2",
                "children": [
                    {
                        "tag": "li",
                        "content": "Item 1",
                        "children": []
                    },
                    {
                        "tag": "li",
                        "content": "Item 2",
                        "children": []
                    },
                    {
                        "tag": "li",
                        "content": "Item 3",
                        "children": []
                    }
                ]
            }
        ]
    },
    {
        "tag": "h2",
        "content": "Subtitle 3",
        "children": [
            {
                "tag": "li",
                "content": "Item 1",
                "children": []
            },
            {
                "tag": "li",
                "content": "Item 2",
                "children": []
            }
        ]
    },
    {
        "tag": "p",
        "content": "This is a paragraph",
        "children": []
    },
    {
        "tag": "h2",
        "content": "Subtitle 4",
        "children": [
            {
                "tag": "li",
                "content": "Item 1",
                "children": []
            },
            {
                "tag": "li",
                "content": "Item 2",
                "children": []
            }
        ]
    }
]
}

Parameters:

Name Type Description Default
markdown str

The Markdown text

required

Returns:

Source code in Docs2KG/modules/llm/markdown2json.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
def openai_layout_json(self, markdown):
    """
    Use OpenAI LLM to convert markdown to json

    The markdown will be converted to json using the OpenAI LLM

    Output format should be like

    Examples:
        {
        "tag": "root",
        "content": {title},
        "children": [
            {
            "tag": "h1",
            "content": "This is the header 1",
            "children": [
                {
                "tag": "h2",
                "content": "This is the header 2",
                "children": [
                    ....
                    {
                    "tag": "p",
                    "content": "This is the paragraph",
                    "children": []
                    }
                ]
                }
            ]
            }
        ]
        }

    For Example:

    ```markdown
    # Title
    ## Subtitle
    ### Subtitle 2
    - Item 1
    - Item 2
    - Item 3

    ## Subtitle 3
    - Item 1
    - Item 2

    This is a paragraph

    ## Subtitle 4
    - Item 1
    - Item 2
    ```

    Should output as

    ```json
    {
    "tag": "h1",
    "content": "Title",
    "children": [
        {
            "tag": "h2",
            "content": "Subtitle",
            "children": [
                {
                    "tag": "h3",
                    "content": "Subtitle 2",
                    "children": [
                        {
                            "tag": "li",
                            "content": "Item 1",
                            "children": []
                        },
                        {
                            "tag": "li",
                            "content": "Item 2",
                            "children": []
                        },
                        {
                            "tag": "li",
                            "content": "Item 3",
                            "children": []
                        }
                    ]
                }
            ]
        },
        {
            "tag": "h2",
            "content": "Subtitle 3",
            "children": [
                {
                    "tag": "li",
                    "content": "Item 1",
                    "children": []
                },
                {
                    "tag": "li",
                    "content": "Item 2",
                    "children": []
                }
            ]
        },
        {
            "tag": "p",
            "content": "This is a paragraph",
            "children": []
        },
        {
            "tag": "h2",
            "content": "Subtitle 4",
            "children": [
                {
                    "tag": "li",
                    "content": "Item 1",
                    "children": []
                },
                {
                    "tag": "li",
                    "content": "Item 2",
                    "children": []
                }
            ]
        }
    ]
    }
    ```

    Args:
        markdown (str): The Markdown text

    Returns:

    """
    messages = [
        {
            "role": "system",
            "content": """You are a helpful assistant to convert markdown to JSON format.

    The markdown given to you will have some noise/not meaningful characters or information, you need to think
    about cleaning the markdown into a cleaned version.

    Then convert the markdown to json

    For Example:

    ```markdown
    # Title
    ## Subtitle
    ### Subtitle 2
    - Item 1
    - Item 2
    - Item 3

    ## Subtitle 3
    - Item 1
    - Item 2

    This is a paragraph

    ## Subtitle 4
    - Item 1
    - Item 2
    ```

    Should output as

    ```json
    {
    "tag": "h1",
    "content": "Title",
    "children": [
        {
            "tag": "h2",
            "content": "Subtitle",
            "children": [
                {
                    "tag": "h3",
                    "content": "Subtitle 2",
                    "children": [
                        {
                            "tag": "li",
                            "content": "Item 1",
                            "children": []
                        },
                        {
                            "tag": "li",
                            "content": "Item 2",
                            "children": []
                        },
                        {
                            "tag": "li",
                            "content": "Item 3",
                            "children": []
                        }
                    ]
                }
            ]
        },
        {
            "tag": "h2",
            "content": "Subtitle 3",
            "children": [
                {
                    "tag": "li",
                    "content": "Item 1",
                    "children": []
                },
                {
                    "tag": "li",
                    "content": "Item 2",
                    "children": []
                },
                {
                    "tag": "p",
                    "content": "This is a paragraph",
                    "children": []
                },
            ]
        },
        {
            "tag": "h2",
            "content": "Subtitle 4",
            "children": [
                {
                    "tag": "li",
                    "content": "Item 1",
                    "children": []
                },
                {
                    "tag": "li",
                    "content": "Item 2",
                    "children": []
                }
            ]
        }
    ]
    }
    ```

    tag will be from the html convention like h1, h2, h3, p, li, etc.

    Keep the meaningful hierarchy information within markdown via the html h1/h2/... tags
    Get them proper in json format.

    If it is a table, leave it as
    {
        "tag": "table",
        "content": "",
        "children": []
    }
    Content should the full content of the table, do not decompose further into tr/td/th, etc

    One example can be
    {
        "tag": "table",
        "content": ",header,header,
                    value,value....
                    ",
        "children": []
    }

    """,
        },
        {
            "role": "user",
            "content": f"Convert the following markdown to JSON format:\n\n{markdown}",
        },
    ]
    return self.llm_openai_call(messages)