当前位置:   article > 正文

如何使用GPT4-o进行few-shot推理_利用gpt进行fewshot

利用gpt进行fewshot

笔者在日常的工作中经常需要使用GPT4-o来进行VQA推理,特别是few-shot推理

现给出推理脚本demo

  1. import base64
  2. import json
  3. from openai import OpenAI
  4. # Initialize OpenAI client
  5. client = OpenAI(api_key="yourkey", base_url="https://openai.com")
  6. # Function to encode the image to base64
  7. def encode_image(image_path):
  8. with open(image_path, "rb") as image_file:
  9. return base64.b64encode(image_file.read()).decode('utf-8')
  10. # Input and output file paths
  11. input_file = 'input.jsonl'
  12. output_file = 'output.jsonl'
  13. # Few-shot examples for image-text interaction
  14. few_shot_examples = [
  15. {
  16. "prompt": "Please describe the object inside the red rectangle in the image and explain why it affect ego car driving.",
  17. "image_path": "",
  18. "answer": "This object is a traffic sign with directional arrows and supplementary plates. The sign shows three arrows indicating lane directions: the left arrow directs traffic to turn left, the central arrow indicates that the lane goes straight ahead, and the right arrow signifies a lane for turning right. The plates below the arrows display speed limits and vehicle classification restrictions. The presence of this sign guides the ego car to choose the correct lane based on its intended route. If the ego car intends to proceed straight, it should align with the central arrow. The speed limit and vehicle classification signs instruct the driver to adhere to the indicated speed limit and lane usage based on the type of vehicle they are operating."
  19. }
  20. ]
  21. # Prepare few-shot examples for GPT-4 input format
  22. few_shot_prompts = [
  23. {
  24. "role": "user",
  25. "content": [
  26. {
  27. "type": "text",
  28. "text": ex["prompt"]
  29. },
  30. {
  31. "type": "image_url",
  32. "image_url": {
  33. "url": f"data:image/jpeg;base64,{encode_image(ex['image_path'])}",
  34. "detail": "high"
  35. }
  36. },
  37. {
  38. "type": "text",
  39. "text": ex["answer"]
  40. }
  41. ]
  42. }
  43. for ex in few_shot_examples
  44. ]
  45. # Open input and output files
  46. with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
  47. for line in infile:
  48. # Parse JSON data from current line
  49. data = json.loads(line)
  50. # Extract information
  51. question_id = data['question_id']
  52. image_path = data['image']
  53. question_text = data['question']
  54. base64_image = encode_image(image_path)
  55. # Prepare messages for the GPT-4 API request
  56. messages = [
  57. {"role": "system", "content": "You are an autonomous driving expert, specializing in recognizing traffic scenes and making driving decisions."},
  58. {"role": "user",
  59. "content": [
  60. {
  61. "type": "text",
  62. "text": question_text
  63. },
  64. {
  65. "type": "image_url",
  66. "image_url": {
  67. "url": f"data:image/jpeg;base64,{base64_image}",
  68. "detail": "high"
  69. }
  70. }
  71. ]
  72. }
  73. ]
  74. # Incorporate few-shot examples into the messages
  75. messages.extend(few_shot_prompts)
  76. # Request completion from GPT-4 API using few-shot method
  77. response = client.chat.completions.create(
  78. model="gpt-4o-2024-05-13",
  79. messages=messages,
  80. stream=False
  81. )
  82. # Extract model response from API response
  83. model_response = response.choices[0].message.content
  84. # Print model response (for debugging purposes)
  85. print(f"Question ID: {question_id}\nQuestion: {question_text}\nAnswer: {model_response}\n")
  86. # Add answer field to data
  87. data['answer'] = model_response
  88. # Write updated data back to output JSONL file
  89. outfile.write(json.dumps(data) + '\n')
  90. print("Processing completed. Answers added to each entry and saved to", output_file)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/943526
推荐阅读
相关标签
  

闽ICP备14008679号