Skip to content

Commit

Permalink
feat: added example with validator in ollama lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper Bollen authored and Casper Bollen committed Mar 8, 2024
1 parent 0532644 commit fbcf902
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 27 deletions.
172 changes: 172 additions & 0 deletions src/Informedica.Ollama.Lib/Notebooks/Validation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"polyglot_notebook": {
"kernelName": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"#load \"load.fsx\"\n",
"\n",
"open Newtonsoft.Json\n",
"open Informedica.Utils.Lib\n",
"open Informedica.Utils.Lib.BCL\n",
"open Informedica.Ollama.Lib\n",
"open Ollama.Operators"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"polyglot_notebook": {
"kernelName": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let validator =\n",
" fun s -> \n",
" match s |> String.contains \"3\" with\n",
" | true -> \"1 + 1 could be 3\" |> Ok\n",
" | false -> \"the answer should include the possibility of a '1 + 1 could be 3'\" |> Error"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"polyglot_notebook": {
"kernelName": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Starting conversation with openchat:7b\n",
"\n",
"Options:\n",
"{\"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}\n",
"\n",
"Got an answer\n",
"\n",
"## Question:\n",
"You are a helpful asistent on general topics\n",
"\n",
"## Answer:\n",
"It seems like you didn't ask any specific question. If you have any topic or question, feel free to ask and I will be happy to help!\n",
"\n",
"\n",
"\n",
"## Question:\n",
"How does the saying go like the whole is more than the individual parts?\n",
"\n",
"## Answer:\n",
"The saying you're referring to is \"The whole is greater than the sum of its parts.\" This phrase means that the overall value, quality, or function of a system, group, or entity is greater than the combined values, qualities, or functions of its individual components. It emphasizes the importance of understanding and appreciating the interconnectedness and synergy within a whole, rather than just focusing on the individual parts.\n",
"\n",
"\n"
]
}
],
"source": [
"let conversation =\n",
" \"You are a helpful asistent on general topics\"\n",
" |> init Ollama.Models.``openchat:7b``\n",
" >>? \"\"\"\n",
" How does the saying go like the whole is more than the individual parts?\n",
" \"\"\"\n",
" |> fun conv -> \n",
" conv |> Ollama.Conversation.print\n",
" conv"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"polyglot_notebook": {
"kernelName": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"## Question:\n",
"You are a helpful asistent on general topics\n",
"\n",
"## Answer:\n",
"It seems like you didn't ask any specific question. If you have any topic or question, feel free to ask and I will be happy to help!\n",
"\n",
"\n",
"\n",
"## Question:\n",
"How does the saying go like the whole is more than the individual parts?\n",
"\n",
"## Answer:\n",
"The saying you're referring to is \"The whole is greater than the sum of its parts.\" This phrase means that the overall value, quality, or function of a system, group, or entity is greater than the combined values, qualities, or functions of its individual components. It emphasizes the importance of understanding and appreciating the interconnectedness and synergy within a whole, rather than just focusing on the individual parts.\n",
"\n",
"\n",
"\n",
"## Question:\n",
"It seems the answer was not correct because: the answer should include the possibility of a '1 + 1 could be 3'\n",
"Can you try again answering?\n",
"\n",
"That would mean 1 + 1 could be ?\n",
"\n",
"## Answer:\n",
"The saying goes, \"The whole is greater than the sum of its parts.\" This means that when you combine individual elements or components together, they create something greater or more significant than what each part contributes individually. In some cases, like with synergy, the whole can be even greater than the simple addition of the parts (e.g., 1 + 1 could be 3).\n",
"\n",
"\n"
]
}
],
"source": [
"conversation\n",
">>! (\"\"\"\n",
"That would mean 1 + 1 could be ?\n",
"\"\"\" |> Ollama.Message.userWithValidator validator)\n",
"|> Ollama.Conversation.print"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 1 addition & 1 deletion src/Informedica.Ollama.Lib/Notebooks/load.fsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# r "nuget: Newtonsoft.Json"

#r "../bin/Debug/net8.0/Informedica.Utils.Lib.dll"
#r "../bin/Debug/net8.0/Informedica.Ollama.Lib.dll"

#load "../Ollama.fs"
56 changes: 30 additions & 26 deletions src/Informedica.Ollama.Lib/Ollama.fs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ module Ollama =
let user = create Result.Ok Roles.user

let system = create Result.Ok Roles.system

let userWithValidator validator = create validator Roles.user


type Response =
Expand Down Expand Up @@ -433,18 +435,24 @@ Options:
}


let (>>?) (conversation : Conversation) msg =

let rec loop tryAgain conversation msg =
let msg = msg |> Message.user

msg
|> run conversation.Model (conversation.Messages |> List.map (_.Question))
|> fun msgs ->
let answer = msgs |> List.last

match answer.Content |> msg.Validator with
| Ok _ ->
let rec private loop tryAgain conversation msg =
msg
|> run conversation.Model (conversation.Messages |> List.map (_.Question))
|> fun msgs ->
let answer = msgs |> List.last

match answer.Content |> msg.Validator with
| Ok _ ->
{ conversation with
Messages =
[{
Question = msg
Answer = answer
}]
|> List.append conversation.Messages
}
| Result.Error err ->
if not tryAgain then
{ conversation with
Messages =
[{
Expand All @@ -453,24 +461,20 @@ Options:
}]
|> List.append conversation.Messages
}
| Result.Error err ->
if not tryAgain then
{ conversation with
Messages =
[{
Question = msg
Answer = answer
}]
|> List.append conversation.Messages
}

else
$"""

else
$"""
It seems the answer was not correct because: {err}
Can you try again answering?
{msg.Content}
"""
|> loop false conversation
|> Message.user
|> loop false conversation


let (>>?) conversation msg =
let msg = msg |> Message.user
loop true conversation msg


let (>>!) conversation msg = loop true conversation msg

0 comments on commit fbcf902

Please sign in to comment.