Skip to content

Commit

Permalink
fix: missing not valid response
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper Bollen authored and Casper Bollen committed Mar 10, 2024
1 parent 3bee916 commit 7798106
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 33 deletions.
24 changes: 19 additions & 5 deletions src/Informedica.Ollama.Lib/Notebooks/Validation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
Expand All @@ -27,7 +27,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 2,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
Expand All @@ -50,7 +50,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 3,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
Expand Down Expand Up @@ -106,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 4,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
Expand Down Expand Up @@ -141,13 +141,27 @@
"\n",
"\n",
"## Question:\n",
"That would mean 1 + 1 could be ?\n",
"\n",
"## Answer:\n",
"The saying \"the whole is more than the sum of its parts\" means that the combination of elements in a whole can create something greater or different from what the individual elements are on their own. In the case of 1 + 1, it would simply equal 2. However, if you were to apply this concept to other contexts, such as teamwork or synergy, the whole could indeed be more than just the sum of its 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",
"Apologies for the confusion earlier. The saying \"the whole is more than the individual parts\" implies that the combination of elements creates a result greater than the sum of its individual components. In the context of your question, if we consider the number 1 as an element, then adding two 1s together would indeed result in a value greater than their individual sum:\n",
"\n",
"1 + 1 = 2\n",
"\n",
"However, if you are referring to the concept of synergy or the whole being greater than the sum of its parts, it could be interpreted as:\n",
"\n",
"1 + 1 = 3 (when considering synergistic effects)\n",
"\n",
"\n"
]
Expand Down
62 changes: 39 additions & 23 deletions src/Informedica.Ollama.Lib/Ollama.fs
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,21 @@ module Ollama =
let user = create Result.Ok Roles.user

let system = create Result.Ok Roles.system


let okMessage = create Ok

let userWithValidator validator = create validator Roles.user


type Response =
| Success of ModelResponse
| Error of string
and ModelResponse = {
error: string
model: string
created_at: string
response: string
message: Message
message: {| role : string; content : string |}
``done``: bool
context: int list
total_duration: int64
Expand Down Expand Up @@ -241,11 +244,21 @@ module Ollama =

let modelResponse =
try
responseBody
|> JsonConvert.DeserializeObject<ModelResponse>
|> Success
let resp =
responseBody
|> JsonConvert.DeserializeObject<ModelResponse>

match resp.error with
| s when s |> String.IsNullOrEmpty ->
responseBody
|> JsonConvert.DeserializeObject<ModelResponse>
|> Success
| s ->
s |> Error
with
| e -> e.ToString() |> Error
| e ->
e.ToString() |> Error

return modelResponse
}

Expand Down Expand Up @@ -277,9 +290,17 @@ module Ollama =

let modelResponse =
try
responseBody
|> JsonConvert.DeserializeObject<ModelResponse>
|> Success
let resp =
responseBody
|> JsonConvert.DeserializeObject<ModelResponse>

match resp.error with
| s when s |> String.IsNullOrEmpty ->
responseBody
|> JsonConvert.DeserializeObject<ModelResponse>
|> Success
| s ->
s |> Error
with
| e ->
e.ToString() |> Error
Expand Down Expand Up @@ -362,7 +383,8 @@ module Ollama =
|> Async.RunSynchronously
|> function
| Success response ->
[message; response.message]

[ message; Message.okMessage response.message.role response.message.content ]
|> List.append messages
| Error s ->
printfn $"oops: {s}"
Expand Down Expand Up @@ -441,8 +463,7 @@ Options:
|> fun msgs ->
let answer = msgs |> List.last

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


match answer.Content |> msg.Validator with
| Ok _ -> newConv
| Result.Error err ->
if not tryAgain then newConv
else
$"""
It seems the answer was not correct because: {err}
Can you try again answering?
{msg.Content}
"""
|> Message.user
|> loop false conversation
|> loop false newConv


let (>>?) conversation msg =
Expand Down
33 changes: 28 additions & 5 deletions src/Informedica.Ollama.Lib/Scripts/AI.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ module Ollama =
and QuestionAnswer =
{
Question : Message
Answer : Message
Answer : Message option
}


Expand All @@ -330,7 +330,7 @@ module Ollama =
{qAndA.Question.Content.Trim()}
## Answer:
{qAndA.Answer.Content.Trim()}
{if qAndA.Answer.IsSome then qAndA.Answer.Value.Content.Trim() else ""}
"""

Expand Down Expand Up @@ -666,7 +666,7 @@ Options:
Messages =
[{
Question = msg
Answer = msgs |> List.last
Answer = msgs |> List.tryLast
}]
}

Expand All @@ -687,7 +687,7 @@ Options:
Messages =
[{
Question = msg
Answer = answer
Answer = Some answer
}]
|> List.append conversation.Messages
}
Expand All @@ -697,7 +697,7 @@ Options:
Messages =
[{
Question = msg
Answer = answer
Answer = Some answer
}]
|> List.append conversation.Messages
}
Expand Down Expand Up @@ -874,3 +874,26 @@ Leg aan ouders uit dat hun kind aan de beademing moet worden gelegd en daarvoor
geintubeerd moet worden.
"""
|> Ollama.Conversation.print

let x =
"""
Je bent een empathische zorgverlener die ouders uitleg moet geven over hun kind
dat op de kinder IC ligt.
Je geeft alle uitleg en antwoorden in het Nederlands.
"""
|> init Ollama.Models.``openchat:7b``


"""
Je bent een empathische zorgverlener die ouders uitleg moet geven over hun kind
dat op de kinder IC ligt.
Je geeft alle uitleg en antwoorden in het Nederlands.
"""
|> Ollama.Message.system
|> Ollama.chat Ollama.Models.llama2 []
|> Async.RunSynchronously


Ollama.listModels ()

0 comments on commit 7798106

Please sign in to comment.