forked from lamg/r0b0t
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.fsx
66 lines (51 loc) · 1.39 KB
/
test.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#r "Lib/bin/Debug/net8.0/Lib.dll"
#r "nuget: FSharp.Control.AsyncSeq, 3.2.1"
#r "nuget: FsHttp"
#r "nuget: LamgEnv, 0.0.2"
#r "nuget: dotenv.net"
open FSharp.Control
open System
open FsHttp
open FSharp.Control
open ProviderModuleImpl.ServerSentEvents
open Stream.Types
dotenv.net.DotEnv.Load()
let key = LamgEnv.getEnv "huggingface_key" |> _.Value
let appendNone (xs: AsyncSeq<'a option>) =
AsyncSeq.append xs (AsyncSeq.ofSeq [ None ])
type Message =
{ index: int
token: {| text: string |} }
let deserializeActive (json: string) =
try
System.Text.Json.JsonSerializer.Deserialize<Message> json |> Some
with _ ->
None
let eventToMsg (line: EventLine) =
match line with
| Data d ->
match deserializeActive d with
| Some { token = t } -> Some(Some t.text)
| _ -> None
| _ -> Some None
let procEventLines (xs: AsyncSeq<EventLine>) =
xs |> AsyncSeq.choose eventToMsg |> AsyncSeq.skipWhile _.IsNone
let ask (key: string) (question: string) =
http {
POST "https://api-inference.huggingface.co/models/microsoft/phi-2"
AuthorizationBearer key
body
jsonSerialize {| inputs = question; stream = true |}
}
|> Request.send
// |> Response.print
// |> printfn "%s"
|> Response.toStream
|> readEvents
|> procEventLines
|> AsyncSeq.iter (function
| Some s -> printf $"{s}"
| _ -> ())
|> Async.RunSynchronously
ask key "Cómo te llamas"
printfn ""