英文:
How do I extract only code content from chat gpt response?
问题
我使用 chatGpt
利用 openai
API (/v1/chat/completions
) 和 gpt-3.5-turbo
作为模型来生成 SQL 查询。
但是我在从响应中提取 SQL 查询时遇到了困难。因为有时 chatGpt 会对查询进行解释,有时则不会。我尝试使用正则表达式,但这并不可靠。
regex = r"SELECT .*?;"
match = re.search(regex, result)
if match:
sql_query = match.group()
print(sql_query)
有没有其他方法来仅提取响应中的代码部分?
英文:
I use chatGpt
to generate SQL query using openai
api(/v1/chat/completions
) and gpt-3.5-turbo
as the model.
But I am facing difficulty in extracting SQL query from the response. Because sometime chatGpt will provide some explanation for query sometimes not. I have tried with regex expressions, but it is not reliable.
regex = r"SELECT .*?;"
match = re.search(regex, result)
if match:
sql_query = match.group()
print(sql_query)
Is there any other approach to extract only the code section from the response?
答案1
得分: 4
我添加了"不要在回答中包含任何解释",并且只得到了代码作为输出。
英文:
I added "don't include any explanations in your responses" and got only the code as output.
答案2
得分: 2
"我添加了“不要在回复中包含问题”并且只得到了代码作为输出。"
英文:
I added "don't include the question in your response" and got only the code as output.
答案3
得分: 1
以下是要翻译的内容:
"Try to write a good prompt; if it still doesn't give you the output in the required format, try to provide examples to try few-shot prompting.
See the below request body,
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a SQL query generation assistant. You will be given a description of the table and a text description on what needs to be extracted or updated. Respond with only the query without explanation"
},
{
"role": "user",
"content": "There is a MySQL database table 'person' with four columns 'id', 'first_name', 'last_name', and 'address'. Write a query to return all entries of the table"
},
{
"role": "assistant",
"content": "SELECT * FROM person;"
},
{
"role": "user",
"content": "Write a query to update the address of the user with the id 103"
}
],
"temperature": 0.8
}
Here, I am giving a simple example response from the model as the context to my actual prompt. If the required output format is complex, you might need to give a few examples.
Below is the response I received for the above request.
{
"id": "chatcmpl-7698KLZnVNmPUHMXx7R4",
"object": "chat.completion",
"created": 1687816228,
"model": "gpt-3.5-turbo-0301",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "UPDATE person SET address = 'New Address' WHERE id = 103;"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 117,
"completion_tokens": 15,
"total_tokens": 132
}
}"
英文:
Try to write a good prompt; if it still doesn't give you the output in the required format, try to provide examples to try few-shot prompting.
See the below request body,
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a SQL query generation assistant. You will be given a description of the table and a text description on what needs to be extracted or updated. Respond with only the query without explanation"
},
{
"role": "user",
"content": "There is a MySQL database table 'person' with four columns 'id', 'first_name', 'last_name' and 'address'. Write a query to return all entries of the table"
},
{
"role": "assistant",
"content": "SELECT * FROM person;"
},
{
"role": "user",
"content": "Write a query to update the address of the user with the id 103"
}
],
"temperature": 0.8
}
Here, I am giving a simple example response from the model as the context to my actual prompt. If the required output format is complex, you might need to give a few examples.
Below is the response I received for the above request.
{
"id": "chatcmpl-7698KLZnVNmPUHMXx7R4",
"object": "chat.completion",
"created": 1687816228,
"model": "gpt-3.5-turbo-0301",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "UPDATE person SET address = 'New Address' WHERE id = 103;"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 117,
"completion_tokens": 15,
"total_tokens": 132
}
}
答案4
得分: 0
我不断地为此而苦恼。我发现这相当可靠地起作用。
英文:
I struggle with this endlessly. I found this works reasonably reliably..
-- Return only the code in the completion. I don't want any other comments. Don't say "here is your code" or similar remarks.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论