Skip to content

Commit

Permalink
Create automate.py
Browse files Browse the repository at this point in the history
Creating a loop that uses both the URL and question, making an iteration that sends the question to the API and receives a response before merging the responses into the final Json.
  • Loading branch information
zi9987 authored Nov 7, 2024
1 parent 024db53 commit 751d860
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions automate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import requests
import json

# Load question examples from JSON file
with open('/content/questions_example.json', 'r', encoding='utf-8') as file:
questions = json.load(file)['questions'] # Access the list of questions directly

# Initialize the output list
output_data = []

# URL for the API endpoint
url = "http://127.0.0.1:5002/api/chat"

# Iterate through each question example
for question in questions:
print("讀取到的 JSON:", question) # Print each loaded JSON object

# Send POST request to the API
response = requests.post(url, json=question)

# Check if the request was successful
if response.status_code == 200:
response_json = response.json()
output_data.append(response_json)
print("成功取得 JSON:", response_json)
else:
print("請求失敗,狀態碼:", response.status_code)

# Save the combined output to a JSON file
with open('pred_retrieve.json', 'w', encoding='utf-8') as output_file:
json.dump(output_data, output_file, ensure_ascii=False, indent=4)

print("合併輸出已保存到 pred_retrieve.json 文件中。")

0 comments on commit 751d860

Please sign in to comment.