-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: enabling structured type output
- Loading branch information
Casper Bollen
authored and
Casper Bollen
committed
Mar 13, 2024
1 parent
d8a79cb
commit 8c7d9ff
Showing
3 changed files
with
326 additions
and
17 deletions.
There are no files selected for viewing
270 changes: 270 additions & 0 deletions
270
src/Informedica.OpenAI.Lib/Notebooks/StructuredJSON.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Using Ollama to get structured output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"dotnet_interactive": { | ||
"language": "fsharp" | ||
}, | ||
"polyglot_notebook": { | ||
"kernelName": "fsharp" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"#load \"load.fsx\"\n", | ||
"\n", | ||
"open Informedica.OpenAI.Lib\n", | ||
"open Ollama.Operators" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Define a schema and a type for the output\n", | ||
"\n", | ||
"The json function will output the type used as a type parameter. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": { | ||
"dotnet_interactive": { | ||
"language": "fsharp" | ||
}, | ||
"polyglot_notebook": { | ||
"kernelName": "fsharp" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"ℹ INFO: \n", | ||
"EndPoint: http://localhost:11434/api/chat\n", | ||
"Payload:\n", | ||
"{\"format\":\"json\",\"messages\":[{\"content\":\"\\nUse schema: { number: int; unit: string }\\nWhat is the minimal corrected gestational age mentioned in the text between '''\\n\\n'''A neonate 28 weeks to 32 weeks corrected gestational age.'''\\n\\nReply in JSON.\",\"role\":\"user\"}],\"model\":\"llama2\",\"options\":{\"num_keep\":null,\"seed\":101,\"num_predict\":null,\"top_k\":null,\"top_p\":null,\"tfs_z\":null,\"typical_p\":null,\"repeat_last_n\":64,\"temperature\":0.0,\"repeat_penalty\":null,\"presence_penalty\":null,\"frequency_penalty\":null,\"mirostat\":0,\"mirostat_tau\":null,\"mirostat_eta\":null,\"penalize_newline\":null,\"stop\":[],\"numa\":null,\"num_ctx\":2048,\"num_batch\":null,\"num_gqa\":null,\"num_gpu\":null,\"main_gpu\":null,\"low_vram\":null,\"f16_kv\":null,\"vocab_only\":null,\"use_mmap\":null,\"use_mlock\":null,\"rope_frequency_base\":null,\"rope_frequency_scale\":null,\"num_thread\":null},\"response_format\":{\"schema\":{\n", | ||
" \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n", | ||
" \"title\": \"<>f__AnonymousType3408661278OfIntegerAndString\",\n", | ||
" \"type\": \"object\",\n", | ||
" \"additionalProperties\": false,\n", | ||
" \"properties\": {\n", | ||
" \"number\": {\n", | ||
" \"type\": \"integer\",\n", | ||
" \"format\": \"int32\"\n", | ||
" },\n", | ||
" \"unit\": {\n", | ||
" \"type\": [\n", | ||
" \"null\",\n", | ||
" \"string\"\n", | ||
" ]\n", | ||
" }\n", | ||
" }\n", | ||
"},\"type\":\"json_object\"},\"stream\":false}\n", | ||
"\n", | ||
"{ number = 28\n", | ||
" unit = \"weeks\" }\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"\n", | ||
"\"\"\"\n", | ||
"Use schema: { number: int; unit: string }\n", | ||
"What is the minimal corrected gestational age mentioned in the text between '''\n", | ||
"\n", | ||
"'''A neonate 28 weeks to 32 weeks corrected gestational age.'''\n", | ||
"\n", | ||
"Reply in JSON.\"\"\"\n", | ||
"|> Message.user\n", | ||
"|> Ollama.json<{| number: int; unit: string |}>\n", | ||
" Ollama.Models.llama2\n", | ||
" []\n", | ||
"|> Async.RunSynchronously\n", | ||
"|> function\n", | ||
" | Ok x -> printfn $\"{x}\"\n", | ||
" | Error e -> printfn $\"oops: {e}\"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Getting the maximum age as a json structure" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": { | ||
"dotnet_interactive": { | ||
"language": "fsharp" | ||
}, | ||
"polyglot_notebook": { | ||
"kernelName": "fsharp" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"ℹ INFO: \n", | ||
"EndPoint: http://localhost:11434/api/chat\n", | ||
"Payload:\n", | ||
"{\"format\":\"json\",\"messages\":[{\"content\":\"\\nUse schema: { number: int; unit: string }\\nWhat is the maximum corrected gestational age mentioned in the text between '''\\n\\n'''A neonate 28 weeks to 32 weeks corrected gestational age.'''\\n\\nReply in JSON.\",\"role\":\"user\"}],\"model\":\"llama2\",\"options\":{\"num_keep\":null,\"seed\":101,\"num_predict\":null,\"top_k\":null,\"top_p\":null,\"tfs_z\":null,\"typical_p\":null,\"repeat_last_n\":64,\"temperature\":0.0,\"repeat_penalty\":null,\"presence_penalty\":null,\"frequency_penalty\":null,\"mirostat\":0,\"mirostat_tau\":null,\"mirostat_eta\":null,\"penalize_newline\":null,\"stop\":[],\"numa\":null,\"num_ctx\":2048,\"num_batch\":null,\"num_gqa\":null,\"num_gpu\":null,\"main_gpu\":null,\"low_vram\":null,\"f16_kv\":null,\"vocab_only\":null,\"use_mmap\":null,\"use_mlock\":null,\"rope_frequency_base\":null,\"rope_frequency_scale\":null,\"num_thread\":null},\"response_format\":{\"schema\":{\n", | ||
" \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n", | ||
" \"title\": \"<>f__AnonymousType3408661278OfIntegerAndString\",\n", | ||
" \"type\": \"object\",\n", | ||
" \"additionalProperties\": false,\n", | ||
" \"properties\": {\n", | ||
" \"number\": {\n", | ||
" \"type\": \"integer\",\n", | ||
" \"format\": \"int32\"\n", | ||
" },\n", | ||
" \"unit\": {\n", | ||
" \"type\": [\n", | ||
" \"null\",\n", | ||
" \"string\"\n", | ||
" ]\n", | ||
" }\n", | ||
" }\n", | ||
"},\"type\":\"json_object\"},\"stream\":false}\n", | ||
"\n", | ||
"{ number = 28\n", | ||
" unit = \"weeks\" }\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"\"\"\"\n", | ||
"Use schema: { number: int; unit: string }\n", | ||
"What is the maximum corrected gestational age mentioned in the text between '''\n", | ||
"\n", | ||
"'''A neonate 28 weeks to 32 weeks corrected gestational age.'''\n", | ||
"\n", | ||
"Reply in JSON.\"\"\"\n", | ||
"|> Message.user\n", | ||
"|> Ollama.json<{| number: int; unit: string |}>\n", | ||
" Ollama.Models.llama2\n", | ||
" []\n", | ||
"|> Async.RunSynchronously\n", | ||
"|> function\n", | ||
" | Ok x -> printfn $\"{x}\"\n", | ||
" | Error e -> printfn $\"oops: {e}\"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Try a different structured output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": { | ||
"dotnet_interactive": { | ||
"language": "fsharp" | ||
}, | ||
"polyglot_notebook": { | ||
"kernelName": "fsharp" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"ℹ INFO: \n", | ||
"EndPoint: http://localhost:11434/api/chat\n", | ||
"Payload:\n", | ||
"{\"format\":\"json\",\"messages\":[{\"content\":\"\\nUse schema: { minAge: int; maxAge: int; unit: string }\\nWhat is corrected gestational age range mentioned in the text between '''\\n\\n'''A neonate 28 weeks to 32 weeks corrected gestational age.'''\\n\\nReply in JSON.\",\"role\":\"user\"}],\"model\":\"llama2\",\"options\":{\"num_keep\":null,\"seed\":101,\"num_predict\":null,\"top_k\":null,\"top_p\":null,\"tfs_z\":null,\"typical_p\":null,\"repeat_last_n\":64,\"temperature\":0.0,\"repeat_penalty\":null,\"presence_penalty\":null,\"frequency_penalty\":null,\"mirostat\":0,\"mirostat_tau\":null,\"mirostat_eta\":null,\"penalize_newline\":null,\"stop\":[],\"numa\":null,\"num_ctx\":2048,\"num_batch\":null,\"num_gqa\":null,\"num_gpu\":null,\"main_gpu\":null,\"low_vram\":null,\"f16_kv\":null,\"vocab_only\":null,\"use_mmap\":null,\"use_mlock\":null,\"rope_frequency_base\":null,\"rope_frequency_scale\":null,\"num_thread\":null},\"response_format\":{\"schema\":{\n", | ||
" \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n", | ||
" \"title\": \"<>f__AnonymousType1996685024OfIntegerAndIntegerAndString\",\n", | ||
" \"type\": \"object\",\n", | ||
" \"additionalProperties\": false,\n", | ||
" \"properties\": {\n", | ||
" \"maxAge\": {\n", | ||
" \"type\": \"integer\",\n", | ||
" \"format\": \"int32\"\n", | ||
" },\n", | ||
" \"minAge\": {\n", | ||
" \"type\": \"integer\",\n", | ||
" \"format\": \"int32\"\n", | ||
" },\n", | ||
" \"unit\": {\n", | ||
" \"type\": [\n", | ||
" \"null\",\n", | ||
" \"string\"\n", | ||
" ]\n", | ||
" }\n", | ||
" }\n", | ||
"},\"type\":\"json_object\"},\"stream\":false}\n", | ||
"\n", | ||
"{ maxAge = 32\n", | ||
" minAge = 28\n", | ||
" unit = \"weeks\" }\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"\"\"\"\n", | ||
"Use schema: { minAge: int; maxAge: int; unit: string }\n", | ||
"What is corrected gestational age range mentioned in the text between '''\n", | ||
"\n", | ||
"'''A neonate 28 weeks to 32 weeks corrected gestational age.'''\n", | ||
"\n", | ||
"Reply in JSON.\"\"\"\n", | ||
"|> Message.user\n", | ||
"|> Ollama.json<{| minAge: int; maxAge: int; unit: string |}>\n", | ||
" Ollama.Models.llama2\n", | ||
" []\n", | ||
"|> Async.RunSynchronously\n", | ||
"|> function\n", | ||
" | Ok x -> printfn $\"{x}\"\n", | ||
" | Error e -> printfn $\"oops: {e}\"\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": ".NET (C#)", | ||
"language": "C#", | ||
"name": ".net-csharp" | ||
}, | ||
"language_info": { | ||
"name": "polyglot-notebook" | ||
}, | ||
"polyglot_notebook": { | ||
"kernelInfo": { | ||
"defaultKernelName": "csharp", | ||
"items": [ | ||
{ | ||
"aliases": [], | ||
"name": "csharp" | ||
}, | ||
{ | ||
"aliases": [], | ||
"languageName": "fsharp", | ||
"name": "fsharp" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters