Skip to content

Instantly share code, notes, and snippets.

@potatoqualitee
Last active August 22, 2024 10:15
Show Gist options
  • Save potatoqualitee/e7a44c19bbc80f46dc2ef525bc97c98e to your computer and use it in GitHub Desktop.
Save potatoqualitee/e7a44c19bbc80f46dc2ef525bc97c98e to your computer and use it in GitHub Desktop.
Structured Objects in PowerShell using PSOpenAI
############# Install PSOpenAI and set your API key #############
Install-Module PSOpenAI
Import-Module PSOpenAI # only when it's first installed
$env:OPENAI_API_KEY = 'sk-xxxxxxxxxxxxxxxxxxxxxx'
############# Here's some markdown that'd be extracted from a PDF, however you want #############
$markdown = "# Dog Shot Record
2309 Kelley Road, Gulfport, MS, 39501
**CLINIC NAME**
Phone: 123-1234567 - Email: [email protected]
## Dog's Information
- **Name:** Chewy
- **Age:** 5
- **Date of Birth:** February 25, 2014
- **Gender:** Male
- **Breed:** Labrador
- **Color:** Brown
## Owner's Information
- **Name:** Jane Doe
- **Address:** 1375 McDowell Street, Mount Pleasant, TN, 38474
## Immunization Dates
| Vaccine | Immunization Date 1 | Immunization Date 2 | Immunization Date 3 | Veterinarian Name |
|----------------|---------------------|---------------------|---------------------|-------------------|
| Distemper | 02/01/2019 | | | John Smith |
| Measles | 02/01/2019 | | | John Smith |
| Parainfluenza | 02/01/2019 | | | John Smith |
| DHPP | 02/01/2019 | 09/01/2017 | 09/01/2018 | John Smith |
| Bordatella | 02/01/2019 | 09/01/2017 | 09/01/2018 | John Smith |
| Coronavirus | 02/01/2019 | 10/01/2017 | 09/01/2018 | John Smith |
| Leptospirosis | 02/01/2019 | 09/01/2017 | 09/01/2018 | John Smith |
| Lyme disease | 02/01/2019 | 10/01/2017 | 09/01/2018 | John Smith |
**Veterinarian Name:** John Smith
**Veterinarian Signature:**
**Date of Signature:** February 25, 2019"
############# Provide the JSON schema #############
$jsonSchema = '{
"name": "pet",
"strict": true,
"schema": {
"type": "object",
"properties": {
"pet_name": {
"type": "string",
"description": "What is the name of the pet?"
},
"owner_name": {
"type": "string",
"description": "What is the name of the owner of the pet?"
},
"pet_breed": {
"type": "string",
"description": "What is the breed of the pet?"
},
"vaccinations": {
"type": "array",
"items": {
"type": "object",
"properties": {
"vaccine_name": {
"type": "string",
"description": "What is the name of the vaccine administered?"
},
"date_administered_1": {
"type": "string",
"description": "When was the first dose of the vaccine administered?"
},
"date_administered_2": {
"type": "string",
"description": "When was the second dose of the vaccine administered?"
},
"date_administered_3": {
"type": "string",
"description": "When was the third dose of the vaccine administered?"
},
"veterinarian": {
"type": "string",
"description": "Who is the veterinarian that administered the first dose?"
}
},
"required": [
"vaccine_name",
"date_administered_1",
"date_administered_2",
"date_administered_3",
"veterinarian"
],
"additionalProperties": false
},
"description": "What vaccinations were given to the pet, including the vaccine names, administration dates, and veterinarian names?"
}
},
"required": [
"pet_name",
"owner_name",
"pet_breed",
"vaccinations"
],
"additionalProperties": false
}
}'
############# call dat #############
$params = @{
Model = "gpt-4o-2024-08-06"
SystemMessage = "You are an assistant that extracts information from pet vaccination records."
Message = $markdown
Format = "json_schema"
JsonSchema = $jsonSchema
}
$result = Request-ChatCompletion @params
$result.Answers | ConvertFrom-Json
@irwins
Copy link

irwins commented Aug 22, 2024

Hi Chrissie,

Just a heads up, line 55 breaks at pet's. At line 119 $result.Answer
Thanks for this! Definitely a game changer!!! 😃

@potatoqualitee
Copy link
Author

@irwins thank you, fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment