如何从聊天 gpt 响应中仅提取代码内容?

Avi*_*ash 1 sql code-generation openai-api gpt-3 chatgpt-api

我使用api( )chatGpt生成 SQL 查询并将其作为模型。openai/v1/chat/completionsgpt-3.5-turbo

但我在从响应中提取 SQL 查询时遇到困难。因为有时 chatGpt 会为查询提供一些解释,有时则不会。我尝试过使用正则表达式,但它不可靠。

regex = r"SELECT .*?;"
match = re.search(regex, result)
if match:
   sql_query = match.group()
   print(sql_query)
Run Code Online (Sandbox Code Playgroud)

是否有其他方法可以从响应中仅提取代码部分?

Tan*_*dar 5

我添加了“不要在您的回复中包含任何解释”,并且只得到了代码作为输出。