Skip to content

Commit

Permalink
Perplexity support
Browse files Browse the repository at this point in the history
  • Loading branch information
lamg committed Aug 31, 2024
1 parent dbcfb4c commit 2d5d8b7
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 9 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [0.1.6] - 2024-08-31

Added:

- Support for Perplexity, using environment variable `perplexity_key` for the API key
- The supported models are `llama-3.1-sonar-small-128k-online`, `llama-3.1-sonar-large-128k-online` and `llama-3.1-sonar-huge-128k-online`.


# [0.1.5] - 2024-08-25

Fixed:
Expand Down
13 changes: 11 additions & 2 deletions R0b0t.Lib/Configuration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Provider =
| HuggingFace
| Anthropic
| ImaginePro
| Perplexity

type SerializableConf =
{ provider_keys: Map<string, string>
Expand Down Expand Up @@ -46,12 +47,18 @@ let anthropicModels =
"claude-3-haiku-20240307"
"claude-3-opus-20240229" ]

let perplexityModels =
[ "llama-3.1-sonar-small-128k-online"
"llama-3.1-sonar-large-128k-online"
"llama-3.1-sonar-huge-128k-online" ]

let providersModels =
[ OpenAI, openAIModels
Anthropic, anthropicModels
GitHub, githubModels
HuggingFace, huggingFaceModels
ImaginePro, imagineProAiModels ]
ImaginePro, imagineProAiModels
Perplexity, perplexityModels ]

let confDir =
(LamgEnv.getEnv "HOME" |> Option.defaultValue "~") :: [ ".config"; "r0b0t" ]
Expand All @@ -70,7 +77,8 @@ type ConfigurationManager() =
Anthropic, "anthropic_key"
HuggingFace, "huggingface_key"
GitHub, "github_key"
ImaginePro, "imaginepro_key" ]
ImaginePro, "imaginepro_key"
Perplexity, "perplexity_key" ]
|> List.choose (fun (p, var) ->
match LamgEnv.getEnv var with
| Some k -> Some(p, Key k)
Expand Down Expand Up @@ -116,6 +124,7 @@ type ConfigurationManager() =
| Some(p, _) -> eprintfn $"model {model} loaded but not supported by {p}"
| None -> eprintfn $"provider {provider} loaded from configuration, but not supported"
with e ->
// TODO show this to the user in the interface
eprintfn $"failed to load configuration: {e.Message}"
else
this.storeConfiguration ()
Expand Down
1 change: 1 addition & 0 deletions R0b0t.Lib/Core.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ let streamCompletion provider key model prompt =
| HuggingFace -> HuggingFace.ask key model prompt
| Anthropic -> Anthropic.ask key model prompt
| ImaginePro -> ImaginePro.imagine key prompt
| Perplexity -> Perplexity.ask key model prompt

let stream (conf: Configuration) (producer: Event<LlmData>) prompt =
match prompt, Map.tryFind conf.provider conf.keys with
Expand Down
62 changes: 62 additions & 0 deletions R0b0t.Lib/ModelProvider/Perplexity.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module Perplexity

open FsHttp
open ServerSentEvents

open Configuration
open Navigation
open GtkGui


type Usage =
{ prompt_tokens: int
completion_tokens: int
total_tokens: int }

type PerplexityMessage = { role: string; content: string }
type Delta = { role: string; content: string }

type PerplexityChoice =
{ index: int
finish_reason: string option
message: PerplexityMessage
delta: Delta }

type Message =
{ id: string
model: obj option
created: uint64
usage: Usage
object: string
choices: PerplexityChoice list }

let deserializeActive (json: string) =
try
System.Text.Json.JsonSerializer.Deserialize<Message> json |> Some
with _ ->
None


let eventLineToMsg (line: EventLine) =
match line with
| Data d ->
match deserializeActive d with
| Some { choices = x :: _ } -> Some(Word x.delta.content)
| _ -> None
| _ -> None

let ask (Key key) (Model model) (prompt: string) =
http {
POST "https://api.perplexity.ai/chat/completions"
AuthorizationBearer key
body

jsonSerialize
{| model = model
max_tokens = 9000
stream = true
messages = [ {| role = "user"; content = prompt |} ] |}
}
|> Request.send
|> Response.toStream
|> chooseEvents eventLineToMsg
4 changes: 2 additions & 2 deletions R0b0t.Lib/ModelProvider/ServerSentEvents.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ let chooseEvents (f: EventLine -> 'a option) (stream: IO.Stream) =
| Some x -> yield x
| None -> ()
| None -> ()
else
cont <- false
else
cont <- false
with
| :? ArgumentNullException
| :? NullReferenceException
Expand Down
6 changes: 3 additions & 3 deletions R0b0t.Lib/Navigation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let settingGroups =
control = SettingGroup "API key for the selected provider for authorizing the requests" }
request = Skip } |]

let providers = [| OpenAI; Anthropic; HuggingFace; GitHub; ImaginePro |]
let providers = [| OpenAI; Anthropic; HuggingFace; GitHub; ImaginePro; Perplexity |]

let setProviderGroup =
providers
Expand All @@ -53,8 +53,8 @@ let providersModels =
Anthropic, anthropicModels
HuggingFace, huggingFaceModels
GitHub, githubModels
ImaginePro, imagineProAiModels ]

ImaginePro, imagineProAiModels
Perplexity, perplexityModels ]

let setModelsGroup =
providersModels
Expand Down
3 changes: 2 additions & 1 deletion R0b0t.Lib/R0b0t.Lib.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RootNamespace>r0b0t.events</RootNamespace>

<Version>0.1.5</Version>
<Version>0.1.6</Version>
<Authors>Luis Ángel Méndez Gort</Authors>
<PackageLicenseExpression>AGPL-3.0-or-later</PackageLicenseExpression>
<AssemblyName>R0b0t.Lib</AssemblyName>
Expand All @@ -29,6 +29,7 @@
<Compile Include="ModelProvider\Anthropic.fs"/>
<Compile Include="ModelProvider\HuggingFace.fs"/>
<Compile Include="ModelProvider\ImaginePro.fs"/>
<Compile Include="ModelProvider\Perplexity.fs"/>
<Compile Include="Core.fs"/>

</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion r0b0t/r0b0t.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageOutputPath>./nupkg</PackageOutputPath>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<Version>0.1.5</Version>
<Version>0.1.6</Version>
<Authors>Luis Ángel Méndez Gort</Authors>
<PackageLicenseExpression>AGPL-3.0-or-later</PackageLicenseExpression>
<AssemblyName>r0b0t</AssemblyName>
Expand Down

0 comments on commit 2d5d8b7

Please sign in to comment.