Skip to content
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

Financial AI #1427

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ gem "sentry-rails"
gem "aws-sdk-s3", require: false
gem "image_processing", ">= 1.2"

# AI
gem "ruby-openai"

# Other
gem "bcrypt", "~> 3.1"
gem "faraday"
Expand All @@ -50,6 +53,7 @@ gem "redcarpet"
gem "stripe"
gem "intercom-rails"
gem "holidays"
gem "redcarpet"

group :development, :test do
gem "debug", platforms: %i[mri windows]
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ GEM
erubi (1.13.0)
et-orbi (1.2.11)
tzinfo
event_stream_parser (1.0.0)
faker (3.5.1)
i18n (>= 1.8.11, < 2)
faraday (2.12.0)
Expand Down Expand Up @@ -388,6 +389,10 @@ GEM
sorbet-runtime (>= 0.5.10782)
ruby-lsp-rails (0.3.21)
ruby-lsp (>= 0.20.0, < 0.21.0)
ruby-openai (7.3.1)
event_stream_parser (>= 0.3.0, < 2.0.0)
faraday (>= 1)
faraday-multipart (>= 1)
ruby-progressbar (1.13.0)
ruby-vips (2.2.2)
ffi (~> 1.12)
Expand Down Expand Up @@ -508,6 +513,7 @@ DEPENDENCIES
redcarpet
rubocop-rails-omakase
ruby-lsp-rails
ruby-openai
selenium-webdriver
sentry-rails
sentry-ruby
Expand Down
30 changes: 30 additions & 0 deletions app/assets/stylesheets/application.tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,34 @@
.scrollbar::-webkit-scrollbar-thumb:hover {
background: #a6a6a6;
}
}


.loading-ellipsis {
display: inline-flex;
}

.dot {
@apply w-1.5 h-1.5 mx-1 bg-gray-400 rounded-full;
animation: ellipsis 1.5s infinite;
}

.dot:nth-child(2) {
animation-delay: 0.3s;
}

.dot:nth-child(3) {
animation-delay: 0.6s;
}

@keyframes ellipsis {

0%,
100% {
transform: scale(0.8);
}

50% {
transform: scale(1.3);
}
}
73 changes: 73 additions & 0 deletions app/controllers/chats_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class ChatsController < ApplicationController
def index
@chats = Current.user.chats.order(created_at: :desc)
end

def new
@chat = Current.user.chats.new
end

def create
@chat = Current.user.chats.create
@message = @chat.messages.create(
user: Current.user,
content: chat_params[:content],
role: "user"
)

# Stub reply from bot
reply = @chat.messages.create(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be moved to the model to clean things up a bit. Something like Message.create_pending_assistant_message

content: "...",
user: nil,
role: "assistant"
)

ChatJob.perform_later(@chat.id, reply.id)
NameChatJob.perform_later(@chat.id)

respond_to do |format|
format.turbo_stream {
render turbo_stream: turbo_stream.update("chat_content", partial: "chats/chat", locals: { chat: @chat })
}
end
end

def show
@chat = Current.user.chats.find(params[:id])
end

def update
@chat = Current.user.chats.find(params[:id])
@message = @chat.messages.create(
user: Current.user,
content: params[:message][:content],
role: "user"
)

# Stub reply from bot
reply = @chat.messages.create(
content: "...",
user: nil,
role: "assistant"
)
Comment on lines +48 to +52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you move it to the model, that can be reused here.


ChatJob.perform_later(@chat.id, reply.id)
NameChatJob.perform_later(@chat.id)

respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.append("chat_messages", partial: "chats/message", locals: { message: @message }),
turbo_stream.append("chat_messages", partial: "chats/message", locals: { message: reply }),
turbo_stream.replace("chat_form", partial: "chats/form", locals: { chat: @chat })
]
end
end
end

private

def chat_params
params.require(:chat).permit(:content)
end
end
6 changes: 6 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,10 @@ def totals_by_currency(collection:, money_method:, separator: " | ", negate: fal
.map { |_currency, money| format_money(money) }
.join(separator)
end

def markdown(text)
@@parser ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, tables: true)

@@parser.render(text).html_safe
end
end
2 changes: 2 additions & 0 deletions app/helpers/chats_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ChatsHelper
end
Loading
Loading