Skip to content

Rag/rag transition 5 #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: rag/rag-transition-4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions assets/css/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,23 @@
);

@use '~bitstyles/scss/bitstyles/utilities/z-index';

.loader {
width: 1rem;
height: 1rem;
border: 5px solid #9485ff;
border-bottom-color: transparent;
border-radius: 50%;
display: inline-block;
box-sizing: border-box;
animation: rotation 1s linear infinite;
}

@keyframes rotation {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
8 changes: 8 additions & 0 deletions lib/chatbot/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ defmodule Chatbot.Application do
ChatbotWeb.Endpoint
]

:ok =
:telemetry.attach_many(
"rag-handler",
Rag.Telemetry.events(),
&Chatbot.Rag.TelemetryHandler.handle_event/4,
nil
)

# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Chatbot.Supervisor]
Expand Down
8 changes: 8 additions & 0 deletions lib/chatbot/rag/telemetry_handler.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Chatbot.Rag.TelemetryHandler do
alias Phoenix.PubSub

def handle_event(prefix, _measurement, _metadata, _config) do
[:rag, key, event] = prefix
PubSub.broadcast(Chatbot.PubSub, "rag", {key, event})
end
end
22 changes: 21 additions & 1 deletion lib/chatbot_web/live/chat_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ defmodule ChatbotWeb.ChatLive do
import ChatbotWeb.CoreComponents
import BitcrowdEcto.Random, only: [uuid: 0]
alias Chatbot.{Chat, Repo}
alias Phoenix.PubSub

@impl Phoenix.LiveView
def mount(_params, _session, socket) do
if connected?(socket), do: PubSub.subscribe(Chatbot.PubSub, "rag")

socket =
socket
|> stream(:messages, Chat.all_messages())
|> assign(:currently_streamed_response, nil)
|> assign(:current_activity, nil)
|> assign(:form, build_form())

{:ok, socket}
Expand Down Expand Up @@ -41,6 +45,10 @@ defmodule ChatbotWeb.ChatLive do
</div>

<div class="u-grid u-gap-l1 u-margin-l1-top">
<.ui_card :if={@current_activity}>
<span class="loader"></span>
<%= @current_activity %>
</.ui_card>
<.simple_form for={@form} phx-submit="send" class="u-justify-self-end u-width-75">
<.ui_input
type="textarea"
Expand Down Expand Up @@ -150,13 +158,25 @@ defmodule ChatbotWeb.ChatLive do
{:noreply, stream_insert(socket, :messages, completed_message)}
end

def handle_info({:generate_embedding, :start}, socket) do
{:noreply, assign(socket, current_activity: "looking up information")}
end

def handle_info({:retrieve, :stop}, socket) do
{:noreply, assign(socket, current_activity: "generating response")}
end

def handle_info({_key, :exception}, socket) do
{:noreply, assign(socket, current_activity: "an error occurred :(")}
end

def handle_info({_key, _event}, socket) do
{:noreply, socket}
end

@impl true
def handle_async(:rag, _no, socket) do
{:noreply, socket}
{:noreply, assign(socket, current_activity: nil)}
end

defp build_form do
Expand Down
Loading