Breakthrough: Using OpenAPI Schemas to shape ChatGPT responses
Sean Chatman
Available for Staff/Senior Front End Generative AI Web Development (Typescript/React/Vue/Python)
Are you using ChatGPT to create conversational AI responses and want to ensure that your responses are accurate, reliable, and consistent with your business rules? One way to achieve this is by using OpenAPI schemas to constrain your responses.
An OpenAPI schema provides a clear and structured specification of your API's behavior, including the format of the requests and responses. By using OpenAPI schema, you can ensure that your ChatGPT responses always meet specific guidelines or constraints.
In this article, we'll show you how to use OpenAPI schema with ChatGPT to create accurate, reliable, and consistent responses for your bot.
## Step 1: Design Your OpenAPI Schema
To use OpenAPI schema with ChatGPT, you'll need to design your schema first. You can use tools such as Swagger Editor or OpenAPI Designer to design your schema.
Begin by identifying the fields that you want to include in your response. Make sure that your schema includes all necessary fields and that the field types, formats, and constraints are clearly specified. You can also include nested types and arrays if necessary.
For example:
```
openapi: "3.0.0"
info:
?title: Expert Profile Schema
?version: "1.0.0"
?description: |
??This is the schema for an expert profile that is used to provide information about the expert's areas of expertise, experience, and qualifications. The expert profile is used on various platforms to connect with clients and to highlight the areas of expertise.
components:
?schemas:
??ExpertProfile:
???type: object
???required:
????- name
????- title
????- summary
????- contact_information
????- expertise_areas
???properties:
????name:
?????type: string
?????maxLength: 50
?????minLength: 1
?????description: The name of the expert.
????title:
?????type: string
?????maxLength: 100
?????minLength: 10
?????description: The job title of the expert that highlights their area of expertise.
????summary:
?????type: string
?????maxLength: 500
?????minLength: 300
?????description: A brief summary of the expert's skills and experience that highlights their unique value proposition.
????profile_image:
?????type: string
?????format: binary
?????description: A profile image of the expert that provides a personal touch to their profile.
????...
```
## Step 2: Integrate Your Schema into ChatGPT
After designing your OpenAPI schema, you can integrate it into your ChatGPT application to constrain your responses. Start by adding the schema to your project, then include it in your response validation code.
For example:
```python
import openapi_schema_validator
import json
# Load the OpenAPI schema from file or generate it programmatically
with open('/path/to/expert_profile_schema.yaml') as file:
??schema = json.loads(file.read())
# Validate the response using the OpenAPI schema
validator = openapi_schema_validator.validate_spec(spec=schema)
validation_result = validator.validate(data=response)
if validation_result.errors:
??raise ValueError(validation_result.errors)
```
This code loads the OpenAPI schema from file, validates the response object against the schema, and raises an error if any validation errors are found.
## Step 3: Test and Iterate
After integrating your OpenAPI schema into ChatGPT, test your chatbot extensively to ensure that it meets all business rules and constraints specified in the schema. Collect feedback from users and monitor your bot's performance, and adjust your schema as necessary.
By using OpenAPI schema with ChatGPT, you can ensure that your conversational AI responses are accurate, reliable, and consistent with your business rules, providing the best experience for your users.
openapi: "3.0.0"
info:
?title: Consultant Profile Schema
?version: "1.0.0"
?description: |
??This is the schema for the consultant profile that is used to provide information about the consultant's expertise areas, experience, and qualifications. The consultant profile is used on various platforms to connect with clients and to highlight the areas of expertise.
components:
?schemas:
??ConsultantProfile:
???type: object
???properties:
????name:
?????type: string
?????maxLength: 50
?????minLength: 1
?????description: The name of the consultant.
????title:
?????type: string
?????maxLength: 100
?????minLength: 10
?????description: The job title of the consultant that highlights their area of expertise.
????summary:
?????type: string
?????maxLength: 500
?????minLength: 300
?????description: A brief summary of the consultant's skills and experience that highlights their unique value proposition.
????profile_image:
?????type: string
?????format: binary
?????description: A profile image of the consultant that provides a personal touch to their profile.
????contact_information:
?????type: object
?????properties:
??????email:
???????type: string
???????format: email
???????description: The email address of the consultant.
??????phone:
???????type: string
???????pattern: "^\d{10}$"
???????description: The phone number of the consultant in the US format.
??????website:
???????type: string
???????format: uri
???????description: The website of the consultant that provides additional information about their services.
????expertise_areas:
?????type: array
?????items:
??????type: object
??????properties:
???????area:
????????type: string
????????maxLength: 100
????????minLength: 10
????????description: The area of expertise of the consultant, such as "Management Consulting" or "Marketing Strategy".
???????projects:
????????type: array
????????items:
?????????type: string
?????????maxLength: 200
?????????minLength: 50
????????maxItems: 5
????????minItems: 2
????????description: A list of 2-5 projects that demonstrate the consultant's experience in the area of expertise.
????education:
?????type: array
?????items:
??????type: object
??????properties:
???????degree:
????????type: string
????????maxLength: 100
????????minLength: 10
????????description: The degree of the consultant, such as "MBA".
???????institute:
????????type: string
????????maxLength: 200
????????minLength: 10
????????description: The name of the institute where the consultant earned the degree.
???????year:
????????type: integer
????????description: The year when the consultant earned the degree.
????????minimum: 1950
????????maximum: 2021
?????maxItems: 3
?????minItems: 1
?????description: A list of 1-3 degrees that the consultant has earned.
????certifications:
?????type: array
?????items:
??????type: string
??????maxLength: 200
??????minLength: 10
?????maxItems: 5
?????minItems: 1
?????description: A list of 1-5 certifications that the consultant has earned in the area of expertise.
????languages_spoken:
??????type: array
??????items:
???????type: string
???????maxLength: 50
???????minLength: 2
??????maxItems: 5
??????minItems: 1
??????description: A list of 1-5 languages that the consultant speaks fluently.
????past_clients:
??????type: array
??????items:
???????type: string
???????maxLength: 200
???????minLength: 10
??????maxItems: 5
??????minItems: 1
??????description: A list of 1-5 past clients that the consultant has worked with and can provide references for.
---------------------
openapi: "3.0.0"
info:
?title: Consultant Information Schema
?version: "1.0.0"
?description: |
??This is the schema for consultant information that is used to manage their profiles and bookings. The consultant information includes their personal and professional details such as their full name, email, phone number, expertise, hourly rates, and availability.
components:
?schemas:
??Consultant:
???type: object
???properties:
????id:
?????type: integer
?????readOnly: true
?????description: The unique identifier for the consultant.
????full_name:
?????type: string
?????maxLength: 50
?????minLength: 5
?????description: The full name of the consultant.
????email:
?????type: string
?????format: email
?????maxLength: 100
?????description: The email address of the consultant.
????phone_number:
?????type: string
?????pattern: '^\+?[0-9]{8,15}$'
?????description: The phone number of the consultant in international format.
????expertise:
?????type: array
?????items:
??????type: string
??????maxLength: 50
??????minLength: 10
?????maxItems: 5
?????minItems: 1
?????uniqueItems: true
?????description: The areas of expertise of the consultant.
????hourly_rate:
?????type: number
?????format: float
?????minimum: 150.00
?????maximum: 500.00
?????description: The hourly rate of the consultant in USD.
????availability:
?????type: array
?????items:
??????type: object
??????properties:
???????day_of_week:
????????type: string
????????enum: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
????????description: The day of the week.
???????start_time:
????????type: string
????????pattern: '^([0-1][0-9]|[2][0-3]):[0-5][0-9]$'
????????description: The start time of the consultant's availability for the specified day.
???????end_time:
????????type: string
????????pattern: '^([0-1][0-9]|[2][0-3]):[0-5][0-9]$'
????????description: The end time of the consultant's availability for the specified day.
?????maxItems: 7
?????minItems: 1
?????uniqueItems: true
?????description: The availability of the consultant for each day of the week.
????bookings:
?????type: array
?????items:
??????type: object
??????properties:
???????id:
????????type: integer
????????readOnly: true
????????description: The unique identifier for the booking.
???????date:
????????type: string
????????format: date
????????description: The date of the booking.
???????start_time:
????????type: string
????????pattern: '^([0-1][0-9]|[2][0-3]):[0-5][0-9]$'
????????description: The start time of the booking.
???????end_time:
????????type: string
????????pattern: '^([0-1][0-9]|[2][0-3]):[0-5][0-9]$'
????????description: The end time of the booking.
???????client_full_name:
????????type: string
????????maxLength: 50
????????minLength: 5
????????description: The full name of the client that booked the consultant.
???????client_email:
????????type: string
????????format: email
????????maxLength: 100
????????description: The email address of the client that booked the consultant.
???????client_phone_number:
????????type: string
????????pattern: '^\+?[0-9]{8,15}$'
????????description: The phone number of the client that booked the consultant in international format.
?????maxItems: 50
?????minItems: 0
?????description: The list of bookings made by the consultant.
---------------------
openapi: "3.0.0"
info:
?title: Coach Profile Schema
?version: "1.0.0"
?description: |
??This is the schema for the coach profile that is used to provide information about the coach's expertise areas, experience, and qualifications. The coach profile is used on various platforms to connect with clients and to highlight the areas of expertise.
components:
?schemas:
??CoachProfile:
???type: object
???properties:
????name:
?????type: string
?????maxLength: 50
?????minLength: 1
?????description: The name of the coach.
????title:
?????type: string
?????maxLength: 100
?????minLength: 10
?????description: The job title of the coach that highlights their area of expertise.
????summary:
?????type: string
?????maxLength: 500
?????minLength: 300
?????description: A brief summary of the coach's skills and experience that highlights their unique value proposition.
????profile_image:
?????type: string
?????format: binary
?????description: A profile image of the coach that provides a personal touch to their profile.
????contact_information:
?????type: object
?????properties:
??????email:
???????type: string
???????format: email
???????description: The email address of the coach.
??????phone:
???????type: string
???????pattern: "^\d{10}$"
???????description: The phone number of the coach in the US format.
??????website:
???????type: string
???????format: uri
???????description: The website of the coach that provides additional information about their services.
????expertise_areas:
?????type: array
?????items:
??????type: object
??????properties:
???????area:
????????type: string
????????maxLength: 100
????????minLength: 10
????????description: The area of expertise of the coach, such as "Executive Coaching" or "Life Coaching".
???????programs:
????????type: array
????????items:
?????????type: string
?????????maxLength: 200
?????????minLength: 50
????????maxItems: 5
????????minItems: 2
????????description: A list of 2-5 coaching programs that demonstrate the coach's experience in the area of expertise.
????education:
?????type: array
?????items:
??????type: object
??????properties:
???????degree:
????????type: string
????????maxLength: 100
????????minLength: 10
????????description: The degree of the coach, such as "Masters in Psychology".
???????institute:
????????type: string
????????maxLength: 200
????????minLength: 10
????????description: The name of the institute where the coach earned the degree.
???????year:
????????type: integer
????????description: The year when the coach earned the degree.
????????minimum: 1950
????????maximum: 2021
?????maxItems: 3
?????minItems: 1
?????description: A list of 1-3 degrees that the coach has earned.
????certifications:
?????type: array
?????items:
??????type: string
??????maxLength: 200
??????minLength: 10
?????maxItems: 5
?????minItems: 1
?????description: A list of 1-5 certifications that the coach has earned in the area of expertise.
????languages_spoken:
?????type: array
?????items:
??????type: string
??????maxLength: 50
??????minLength: 2
?????maxItems: 5
?????minItems: 1
?????description: A list of 1-5 languages that the coach speaks fluently.
????client_testimonials:
?????type: array
?????items:
??????type: object
??????properties:
???????client_name:
????????type: string
????????maxLength: 50
????????minLength: 1
????????description: The name of the client who provided the testimonial.
???????testimonial:
????????type: string
????????maxLength: 500
????????minLength: 50
????????description: The testimonial provided by the client.
??????maxItems: 5
??????minItems: 1
?????description: A list of 1-5 client testimonials that demonstrate the coach's ability to help clients achieve their goals.
-------------
openapi: "3.0.0"
info:
?title: Expert Profile Schema
?version: "1.0.0"
?description: |
??This is the schema for an expert profile that is used to provide information about the expert's areas of expertise, experience, and qualifications. The expert profile is used on various platforms to connect with clients and to highlight the areas of expertise.
components:
?schemas:
??ExpertProfile:
???type: object
???required:
????- name
????- title
????- summary
????- contact_information
????- expertise_areas
???properties:
????name:
?????type: string
?????maxLength: 50
?????minLength: 1
?????description: The name of the expert.
????title:
?????type: string
?????maxLength: 100
?????minLength: 10
?????description: The job title of the expert that highlights their area of expertise.
????summary:
?????type: string
?????maxLength: 500
?????minLength: 300
?????description: A brief summary of the expert's skills and experience that highlights their unique value proposition.
????profile_image:
?????type: string
?????format: binary
?????description: A profile image of the expert that provides a personal touch to their profile.
????contact_information:
?????type: object
?????properties:
??????email:
???????type: string
???????format: email
???????description: The email address of the expert.
??????phone:
???????type: string
???????pattern: "^\d{10}$"
???????description: The phone number of the expert in the US format.
??????website:
???????type: string
???????format: uri
???????description: The website of the expert that provides additional information about their services.
????expertise_areas:
?????type: array
?????items:
??????type: object
??????properties:
???????area:
????????type: string
????????maxLength: 100
????????minLength: 10
????????description: The area of expertise of the expert, such as "IT Security" or "UX Design".
???????experience:
????????type: integer
????????description: The years of experience the expert has in the area of expertise.
????????minimum: 1
????????maximum: 50
???????skills:
????????type: array
????????items:
?????????type: string
?????????maxLength: 200
?????????minLength: 50
????????maxItems: 10
????????minItems: 2
????????description: A list of 2-10 skills that demonstrate the expert's proficiency in the area of expertise.
????education:
?????type: array
?????items:
??????type: object
??????properties:
???????degree:
????????type: string
????????maxLength: 100
????????minLength: 10
????????description: The degree of the expert, such as "Masters in Computer Science".
???????institute:
????????type: string
????????maxLength: 200
????????minLength: 10
????????description: The name of the institute where the expert earned the degree.
???????year:
????????type: integer
????????description: The year when the expert earned the degree.
????????minimum: 1950
????????maximum: 2021
?????maxItems: 3
?????minItems: 1
?????description: A list of 1-3 degrees that the expert has earned.
????certifications:
?????type: array
?????items:
??????type: string
??????maxLength: 200
??????minLength: 10
?????maxItems: 5
?????minItems: 1
?????description: A list of 1-5 certifications that the expert has earned in the area of expertise.
????languages_spoken:
?????type: array
?????items:
??????type: string
??????maxLength: 50
??????minLength: 2
?????maxItems: 5
?????minItems: 1
?????description: A list of 1-5 languages that the expert speaks fluently.
????client_testimonials:
?????type: array
?????items:
??????type: object
??????properties:
???????client_name:
????????type: string
????????maxLength: 50
????????minLength: 1
????????description: The name of the client who provided the testimonial.
???????testimonial:
????????type: string
????????maxLength: 500
????????minLength: 50
????????description: The testimonial provided by the client.
??????maxItems: 5
??????minItems: 1
?????description: A list of 1-5 client testimonials that demonstrate the expert's ability to help clients achieve their goals.