openai.Completion.create is not giving any text in the response

Here is the code I am trying to run. I am expecting the result to print the matched records, but its not returning anything :
import openai
import json

Set up your OpenAI API key

openai.api_key = “”

Define the filenames for the two files you want to compare

file1 = “bookings1.json”
file2 = “bookings2.json”

Load the data from the two files into Python dictionaries

with open(file1) as f1:
data1 = json.load(f1)

with open(file2) as f2:
data2 = json.load(f2)

Extract the relevant information from each booking and put it into a list

bookings1 =
for booking in data1[‘bookings’]:
booking_info = f"{booking[‘hotel_name’]} {booking[‘arrival_date’]} {booking[‘departure_date’]} {booking[‘guest_name’]}"
bookings1.append(booking_info)

bookings2 =
for booking in data2[‘bookings’]:
booking_info = f"{booking[‘hotel_name’]} {booking[‘arrival_date’]} {booking[‘departure_date’]} {booking[‘guest_name’]}"
bookings2.append(booking_info)

Use OpenAI’s GPT-3 API to compare each booking in file1 with each booking in file2

matches =
for booking1 in bookings1:
for booking2 in bookings2:
prompt = f"Are these two bookings a match?\nBooking 1: {booking1}\nBooking 2: {booking2}\nAnswer:"
response = openai.Completion.create(
engine=“text-davinci-002”,
prompt=prompt,
max_tokens=1,
temperature=0.0,
stop=[“\n”]
)
print(response)
similarity = response.choices[0].text.strip()
if similarity == “1”:
matches.append((booking1, booking2))

Print the matching bookings

for match in matches:
print(f"Match found between booking {match[0]} in {file1} and booking {match[1]} in {file2}.")

Note that the code assumes that each booking in the JSON files contains the keys:
"hotel_name"
"arrival_date"
"departure_date"
"guest_name"
If the JSON data is structured differently, the code may need to be modified to extract the relevant information from each booking.

This is your code in a more readable way, so people can help you better.
The forum uses markdown - use triple backticks (```...code...```) at the beginning and end of any code posted here

import openai
import json

# Set up your OpenAI API key

openai.api_key = “my-api-key”

# Define the filenames for the two files you want to compare

file1 = “bookings1.json”
file2 = “bookings2.json”

# Load the data from the two files into Python dictionaries

with open(file1) as f1:
data1 = json.load(f1)

with open(file2) as f2:
data2 = json.load(f2)

# Extract the relevant information from each booking and put it into a list

bookings1 = []
for booking in data1[‘bookings’]:
booking_info = f"{booking[‘hotel_name’]} {booking[‘arrival_date’]} {booking[‘departure_date’]} {booking[‘guest_name’]}"
bookings1.append(booking_info)

bookings2 = []
for booking in data2[‘bookings’]:
booking_info = f"{booking[‘hotel_name’]} {booking[‘arrival_date’]} {booking[‘departure_date’]} {booking[‘guest_name’]}"
bookings2.append(booking_info)

# Use OpenAI’s GPT-3 API to compare each booking in file1 with each booking in file2

matches = []
for booking1 in bookings1:
for booking2 in bookings2:
prompt = f"Are these two bookings a match?\nBooking 1: {booking1}\nBooking 2: {booking2}\nAnswer:"
response = openai.Completion.create(
engine=“text-davinci-002”,
prompt=prompt,
max_tokens=1,
temperature=0.0,
stop=[“\n”]
)
print(response)
similarity = response.choices[0].text.strip()
if similarity == “1”:
matches.append((booking1, booking2))

# Print the matching bookings

for match in matches:
print(f"Match found between booking {match[0]} in {file1} and booking {match[1]} in {file2}.")

Thanks Alex !

here is how the bookings1.json and bookings2.json look like :slight_smile:
bookings1.json

{
“bookings”: [
{
“hotel_name”: “Hotel A”,
“arrival_date”: “2023-06-01”,
“departure_date”: “2023-06-05”,
“guest_name”: “John Smith”
},
{
“hotel_name”: “Hotel B”,
“arrival_date”: “2023-07-01”,
“departure_date”: “2023-07-05”,
“guest_name”: “Jane Doe”
},
{
“hotel_name”: “Hotel C”,
“arrival_date”: “2023-08-01”,
“departure_date”: “2023-08-05”,
“guest_name”: “Bob Johnson”
}
]
}

bookings2.json

{
“bookings”: [
{
“hotel_name”: “Hotel A”,
“arrival_date”: “2023-06-01”,
“departure_date”: “2023-06-05”,
“guest_name”: “John Smith”
},
{
“hotel_name”: “Hotel B”,
“arrival_date”: “2023-07-01”,
“departure_date”: “2023-07-05”,
“guest_name”: “Mary Lee”
},
{
“hotel_name”: “Hotel D”,
“arrival_date”: “2023-09-01”,
“departure_date”: “2023-09-05”,
“guest_name”: “Tom Wilson”
}
]
}

thanks for the information.
if similarity == “1”:
The similarity is based on the response generated by the API: the response is expected to be a single character, either “0” or “1”. “1” means that the two bookings are considered a match, and “0” means they are not a match - “1” indicates a 100% match, while “0” indicates no match - so in your json-bookings there is no full matches, you are expecting a “0”. Is that correct?

We can confirm this on:
max_tokens=1,
since the expected response is a single character

For testing:

  1. Try increasing the max_tokens parameter to a higher value to see if the model is able to return a response with more context;
  2. Check the response object returned by the API to see if it contains any error messages or other information.

Maybe the model is trying to respond with something else, an issue - but it is limited by the max_tokens.

Try this testing, and let me know.

1 Like

thanks Alex ! increasing the max_tokens did the trick

The text was returning Yes and No instead of 1 and 0.
here is the below print of the response

Hotel A 2023-06-01 2023-06-05 John Smith
Hotel A 2023-06-01 2023-06-05 John Smith
Are these two bookings a match?
Booking 1: Hotel A 2023-06-01 2023-06-05 John Smith
Booking 2: Hotel A 2023-06-01 2023-06-05 John Smith
Answer:
{
“choices”: [
{
“finish_reason”: “stop”,
“index”: 0,
“logprobs”: null,
“text”: " Yes"
}
],
“created”: 1683130267,
“id”: “cmpl-7C9MJBfTJVL7s04X5gcAjVQkc2OQY”,
“model”: “text-davinci-002”,
“object”: “text_completion”,
“usage”: {
“completion_tokens”: 1,
“prompt_tokens”: 53,
“total_tokens”: 54
}
}
Yes
Hotel A 2023-06-01 2023-06-05 John Smith
Hotel B 2023-07-01 2023-07-05 Mary Lee
Are these two bookings a match?
Booking 1: Hotel A 2023-06-01 2023-06-05 John Smith
Booking 2: Hotel B 2023-07-01 2023-07-05 Mary Lee
Answer:
{
“choices”: [
{
“finish_reason”: “length”,
“index”: 0,
“logprobs”: null,
“text”: " No, these two book"
}
],
“created”: 1683130268,
“id”: “cmpl-7C9MKfUikrngUXQyMFrk6O9wpfTUJ”,
“model”: “text-davinci-002”,
“object”: “text_completion”,
“usage”: {
“completion_tokens”: 5,
“prompt_tokens”: 53,
“total_tokens”: 58
}
}
No, these two book
Hotel A 2023-06-01 2023-06-05 John Smith
Hotel D 2023-09-01 2023-09-05 Tom Wilson
Are these two bookings a match?
Booking 1: Hotel A 2023-06-01 2023-06-05 John Smith
Booking 2: Hotel D 2023-09-01 2023-09-05 Tom Wilson
Answer:
{
“choices”: [
{
“finish_reason”: “length”,
“index”: 0,
“logprobs”: null,
“text”: " No, these two book"
}
],
“created”: 1683130268,
“id”: “cmpl-7C9MKN9eQcBnPOFvTeLXp0y1UnLdi”,
“model”: “text-davinci-002”,
“object”: “text_completion”,
“usage”: {
“completion_tokens”: 5,
“prompt_tokens”: 53,
“total_tokens”: 58
}
}
No, these two book
Hotel B 2023-07-01 2023-07-05 Jane Doe
Hotel A 2023-06-01 2023-06-05 John Smith
Are these two bookings a match?
Booking 1: Hotel B 2023-07-01 2023-07-05 Jane Doe
Booking 2: Hotel A 2023-06-01 2023-06-05 John Smith
Answer:
{
“choices”: [
{
“finish_reason”: “length”,
“index”: 0,
“logprobs”: null,
“text”: " No, these two book"
}
],
“created”: 1683130268,
“id”: “cmpl-7C9MKkYxGybxkLAEH8sZMLeb7P6UN”,
“model”: “text-davinci-002”,
“object”: “text_completion”,
“usage”: {
“completion_tokens”: 5,
“prompt_tokens”: 53,
“total_tokens”: 58
}
}
No, these two book
Hotel B 2023-07-01 2023-07-05 Jane Doe
Hotel B 2023-07-01 2023-07-05 Mary Lee
Are these two bookings a match?
Booking 1: Hotel B 2023-07-01 2023-07-05 Jane Doe
Booking 2: Hotel B 2023-07-01 2023-07-05 Mary Lee
Answer:
{
“choices”: [
{
“finish_reason”: “length”,
“index”: 0,
“logprobs”: null,
“text”: " Yes, these two book"
}
],
“created”: 1683130269,
“id”: “cmpl-7C9MLcuKm3SsmaDxG9eoID4hjm3Va”,
“model”: “text-davinci-002”,
“object”: “text_completion”,
“usage”: {
“completion_tokens”: 5,
“prompt_tokens”: 53,
“total_tokens”: 58
}
}
Yes, these two book
Hotel B 2023-07-01 2023-07-05 Jane Doe
Hotel D 2023-09-01 2023-09-05 Tom Wilson
Are these two bookings a match?
Booking 1: Hotel B 2023-07-01 2023-07-05 Jane Doe
Booking 2: Hotel D 2023-09-01 2023-09-05 Tom Wilson
Answer:
{
“choices”: [
{
“finish_reason”: “length”,
“index”: 0,
“logprobs”: null,
“text”: " No, these two book"
}
],
“created”: 1683130270,
“id”: “cmpl-7C9MMrJD4xblfUfCgq9HZEWkyVrXv”,
“model”: “text-davinci-002”,
“object”: “text_completion”,
“usage”: {
“completion_tokens”: 5,
“prompt_tokens”: 53,
“total_tokens”: 58
}
}
No, these two book
Hotel C 2023-08-01 2023-08-05 Bob Johnson
Hotel A 2023-06-01 2023-06-05 John Smith
Are these two bookings a match?
Booking 1: Hotel C 2023-08-01 2023-08-05 Bob Johnson
Booking 2: Hotel A 2023-06-01 2023-06-05 John Smith
Answer:
{
“choices”: [
{
“finish_reason”: “length”,
“index”: 0,
“logprobs”: null,
“text”: “\n\nNo, these”
}
],
“created”: 1683130270,
“id”: “cmpl-7C9MMk8NTldTCjoMWo9riiM89ioIC”,
“model”: “text-davinci-002”,
“object”: “text_completion”,
“usage”: {
“completion_tokens”: 5,
“prompt_tokens”: 53,
“total_tokens”: 58
}
}
No, these
Hotel C 2023-08-01 2023-08-05 Bob Johnson
Hotel B 2023-07-01 2023-07-05 Mary Lee
Are these two bookings a match?
Booking 1: Hotel C 2023-08-01 2023-08-05 Bob Johnson
Booking 2: Hotel B 2023-07-01 2023-07-05 Mary Lee
Answer:
{
“choices”: [
{
“finish_reason”: “length”,
“index”: 0,
“logprobs”: null,
“text”: " No, these two book"
}
],
“created”: 1683130270,
“id”: “cmpl-7C9MMRm32p307keLQdC4mCtGO7m5V”,
“model”: “text-davinci-002”,
“object”: “text_completion”,
“usage”: {
“completion_tokens”: 5,
“prompt_tokens”: 53,
“total_tokens”: 58
}
}
No, these two book
Hotel C 2023-08-01 2023-08-05 Bob Johnson
Hotel D 2023-09-01 2023-09-05 Tom Wilson
Are these two bookings a match?
Booking 1: Hotel C 2023-08-01 2023-08-05 Bob Johnson
Booking 2: Hotel D 2023-09-01 2023-09-05 Tom Wilson
Answer:
{
“choices”: [
{
“finish_reason”: “length”,
“index”: 0,
“logprobs”: null,
“text”: " No, these two book"
}
],
“created”: 1683130271,
“id”: “cmpl-7C9MNShvAoZaonnw9nh8j5iR9jKqS”,
“model”: “text-davinci-002”,
“object”: “text_completion”,
“usage”: {
“completion_tokens”: 5,
“prompt_tokens”: 53,
“total_tokens”: 58
}
}
No, these two book
Match found between booking Hotel A 2023-06-01 2023-06-05 John Smith in bookings1.json and booking Hotel A 2023-06-01 2023-06-05 John Smith in bookings2.json.

This model is giving me exact matches, but I also want to find almost matches with its match probability score. That way I can find near matches as well.
I am very new to this space and hence all this question. Basically I am trying to see if I can find transaction matching. Also how can I constantly train the model with my own datasets ?