Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Created October 13, 2024 16:46
Show Gist options
  • Save isaacabraham/c3573cbfa5ce79fe0050eeab3fcb7808 to your computer and use it in GitHub Desktop.
Save isaacabraham/c3573cbfa5ce79fe0050eeab3fcb7808 to your computer and use it in GitHub Desktop.
#r "nuget: OpenAI, 2.0.0"
open OpenAI.Chat
let client =
ChatClient("gpt-4o-mini", "API-KEY-GOES-HERE")
//1. Basic call
let response =
client.CompleteChat([ UserChatMessage "Please send a funny greeting back to me!" :> ChatMessage ])
//2. Chat history
let responseOfConversation =
client.CompleteChat [
UserChatMessage "Please send a funny greeting back to me!" :> ChatMessage
AssistantChatMessage
"Sure thing! Here you go: Why did the computer go to therapy? Because it had too many bytes of emotional baggage! Hope your day is full of laughter and zero glitches!"
UserChatMessage "Now send me a short one."
]
responseOfConversation.Value.Content[0].Text
//3. Stateful conversation
type Conversation = {
Messages: ResizeArray<ChatMessage>
} with
/// Starts a brand new conversation
static member StartNew() = { Messages = ResizeArray() }
/// Sends a message to the bot and returns the response, storing the response in the conversation value.
member this.SendMessage(message: string) =
this.Messages.Add(UserChatMessage message)
let response = this.Messages |> client.CompleteChat
this.Messages.Add(AssistantChatMessage response.Value)
response.Value.Content[0].Text
/// Returns the history of the conversation, as a tuple array of message type and content.
member this.History() =
this.Messages
|> Seq.map (fun r -> r.GetType().Name, r.Content.[0].Text)
|> Seq.toArray
let conversation = Conversation.StartNew()
conversation.SendMessage "Please send a funny greeting back to me!"
conversation.SendMessage "Now send a short, unfunny one."
conversation.SendMessage "Yes, I'm having a great day thanks! How about you?"
conversation.History()
// Some data-oriented programming
open System.Diagnostics
open System.IO
let fsConversation = Conversation.StartNew()
fsConversation.SendMessage "What is the F# programming language?"
fsConversation.SendMessage "Give me those points as some JSON that I can parse, with properties Name and Description. Don't include any other information or headings - just plain JSON please that can be directly parsed by a machine."
fsConversation.SendMessage "No markdown formatting. PLAIN JSON ONLY."
open System.Text.Json
let data =
JsonSerializer.Deserialize<{| Name: string; Description: string |} array>(
fsConversation.History() |> Seq.item 5 |> snd
)
fsConversation.SendMessage "Now return that as a fully-featured HTML page with styles built on top of the Bulma CSS framework."
open System.Diagnostics
open System.IO
File.WriteAllText("sample.html", fsConversation.History() |> Seq.item 7 |> snd)
Process.Start(ProcessStartInfo("sample.html", UseShellExecute = true)) |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment