Hello - i try to read a txt-file, analyse some data and ouput the result in json-format using the following code:
import os, sys
from urllib.parse import urlparse
import time
from dotenv import load_dotenv
from openai import OpenAI
from pydantic import BaseModel
from typing import List
class ArticleSummary(BaseModel):
category: str
class Messages(BaseModel):
messages: List[ArticleSummary]
path = os.path.abspath(os.path.dirname(sys.argv[0]))
fn = os.path.join(path, ".env")
load_dotenv(fn)
CHATGPT_API_KEY = os.environ.get("CHATGPT_API_KEY")
client = OpenAI(api_key = CHATGPT_API_KEY)
fn = os.path.join(path, "prompt.txt")
with open(fn, encoding="utf-8", errors="ignore") as f:
lines = f.read().splitlines()
question = "\n".join(lines)
### upload txt-file for analysis
fn = os.path.join(path, "categories.txt")
vector_store = client.vector_stores.create(name="File")
file_paths = [fn]
file_streams = [open(path, "rb") for path in file_paths]
file_batch = client.vector_stores.file_batches.upload_and_poll(
vector_store_id=vector_store.id, files=file_streams
)
print(file_batch.status)
print(file_batch.file_counts)
# exit()
resp = client.responses.create(
model="gpt-4o-mini",
input=[{"role": "user", "content": question}],
tools=[{"type": "file_search", "vector_store_ids": [vector_store.id]}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "test_schema",
"schema": Messages.model_json_schema()
}
}
)
resultAnswer = resp.output[-1].content[0].text
fnResult = os.path.join(path, "result.txt")
with open(fnResult, "w", encoding="UTF-8", errors="ignore") as f:
f.write(resultAnswer)
print(f"Result written to {fnResult}")
print(f"Program {os.path.basename(__file__)} finished - will close soon...")
time.sleep(5)
Unfortunately i get this erroor when running the program:
TypeError: Responses.create() got an unexpected keyword argument ‘response_format’
How can i output the result as json-file?