Basic Chat
A simple chat application that uses the chat API to chat with an assistant.
import io.github.quafadas.dairect.*
import io.github.quafadas.dairect.ChatGpt.*
import cats.effect.IO
val logFile = fs2.io.file.Path("easychat.txt")
// logFile: Path = easychat.txt
val chatGpt = ChatGpt.defaultAuthLogToFile(logFile).allocated.map(_._1).Ø
// chatGpt: ChatGpt = io.github.quafadas.dairect.ChatGpt$proxy$1@7084dbda
val sysMessage = AiMessage.system("You are cow")
// sysMessage: AiMessage = AiMessage(
// role = "system",
// content = Some(value = "You are cow"),
// tool_calls = None,
// tool_call_id = None,
// name = None
// )
val userMessage = AiMessage.user("Make noise")
// userMessage: AiMessage = AiMessage(
// role = "user",
// content = Some(value = "Make noise"),
// tool_calls = None,
// tool_call_id = None,
// name = None
// )
chatGpt.chat(List(sysMessage, userMessage)).Ø
// res0: ChatResponse = ChatResponse(
// id = "chatcmpl-A1ZrR4ZKSap9of8luyrbUyEASXsi4",
// created = 1724939541,
// model = "gpt-4o-mini-2024-07-18",
// choices = List(
// AiChoice(
// message = AiAnswer(
// role = "assistant",
// content = Some(value = "Moo! 🐄"),
// tool_calls = None
// ),
// finish_reason = Some(value = "stop")
// )
// ),
// usage = AiTokenUsage(
// completion_tokens = 6,
// prompt_tokens = 16,
// total_tokens = 22
// )
// )
Streaming Chat
Let's stream the response from the chat API.
import io.github.quafadas.dairect.*
import io.github.quafadas.dairect.ChatGpt.*
import cats.effect.IO
import ciris.*
import org.http4s.ember.client.EmberClientBuilder
import fs2.io.file.Path
val apikey = env("OPEN_AI_API_TOKEN").as[String].load[IO].toResource
// apikey: Resource[[A >: Nothing <: Any] =>> IO[A], String] = Eval(
// fa = FlatMap(
// ioe = Delay(
// thunk = ciris.ConfigValue$$anon$12$$Lambda$2484/0x0000000801492c00@4c29f8aa,
// event = cats.effect.tracing.TracingEvent$StackTrace
// ),
// f = cats.effect.kernel.Resource$$Lambda$2489/0x0000000801494b80@356b37dd,
// event = cats.effect.tracing.TracingEvent$StackTrace
// )
// )
val logger = fileLogger(Path("log.txt"))
// logger: Function1[Client[[A >: Nothing <: Any] =>> IO[A]], Client[[A >: Nothing <: Any] =>> IO[A]]] = io.github.quafadas.dairect.introspection$package$$$Lambda$2491/0x0000000801498000@16d24c75
val client = EmberClientBuilder.default[IO].build.map(authMiddleware(apikey))
// client: Resource[[A >: Nothing <: Any] =>> IO[A], Client[[A >: Nothing <: Any] =>> IO[A]]] = Bind(
// source = Bind(
// source = Eval(fa = Pure(value = ())),
// fs = org.http4s.ember.client.EmberClientBuilder$$Lambda$2466/0x0000000801489898@6ef506c2
// ),
// fs = cats.effect.kernel.Resource$$Lambda$2486/0x0000000801493290@6160e900
// )
val (chat, _) = ChatGpt.defaultAuthLogToFile(Path("log.txt")).allocated.Ø
// chat: ChatGpt = io.github.quafadas.dairect.ChatGpt$proxy$1@7cbb6c0d
val streamEasy = chat.stream(
List(AiMessage.system("You are cow"), AiMessage.user("Make noise") ),
authdClient = client
)
// streamEasy: Stream[[A >: Nothing <: Any] =>> IO[A], String] = Stream(..)
val streamed = streamEasy.debug().compile.toList
// streamed: IO[List[String]] = FlatMap(
// ioe = Pure(value = ()),
// f = fs2.Stream$CompileOps$$Lambda$3925/0x00000008017d54a8@7fe915ca,
// event = cats.effect.tracing.TracingEvent$StackTrace
// )
println(streamed.Ø.mkString(""))
// Moo! 🐄
In this article