-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: use llm to generate ideas #328
- Loading branch information
1 parent
8e5d252
commit 8043e15
Showing
28 changed files
with
373 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
lib/mindwendel/services/chat_completions/chat_completions_service.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule Mindwendel.Services.ChatCompletions.ChatCompletionsService do | ||
@callback generate_ideas(String.t()) :: {:error, any()} | {:ok, any()} | ||
@callback enabled?() :: boolean() | ||
|
||
# See https://hexdocs.pm/elixir_mock/getting_started.html for why we are doing it this way: | ||
def generate_ideas(title), do: impl().generate_ideas(title) | ||
def enabled?(), do: impl().enabled? | ||
|
||
defp impl, | ||
do: | ||
Application.get_env( | ||
:mindwendel, | ||
:chat_completions_service, | ||
Mindwendel.Services.ChatCompletions.ChatCompletionsServiceImpl | ||
) | ||
end |
77 changes: 77 additions & 0 deletions
77
lib/mindwendel/services/chat_completions/chat_completions_service_impl.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
defmodule Mindwendel.Services.ChatCompletions.ChatCompletionsServiceImpl do | ||
require Logger | ||
|
||
alias Mindwendel.Services.ChatCompletions.ChatCompletionsService | ||
alias OpenaiEx.Chat | ||
alias OpenaiEx.ChatMessage | ||
|
||
@behaviour ChatCompletionsService | ||
|
||
@impl ChatCompletionsService | ||
def generate_ideas(title) do | ||
case setup_chat_completions_service() do | ||
{:ok, chat_completions_service} -> | ||
chat_req = | ||
Chat.Completions.new( | ||
model: "meta-llama-3-8b-instruct", | ||
messages: [ | ||
ChatMessage.user( | ||
"Generate ONLY json for the following request, no other content. The format should be [{idea: generated_content}]. Replace generated_content." | ||
), | ||
ChatMessage.user("In a brainstorming, generate 5 ideas for the following title:"), | ||
ChatMessage.user(title) | ||
] | ||
) | ||
|
||
ideas = | ||
case chat_completions_service |> Chat.Completions.create(chat_req) do | ||
{:ok, chat_response} -> | ||
choices = List.first(chat_response["choices"]) | ||
Jason.decode!(choices["message"]["content"]) | ||
|
||
{:error, error} -> | ||
Logger.error("Error while fetching ideas from LLM:") | ||
Logger.error(error) | ||
[] | ||
end | ||
|
||
ideas | ||
|
||
_ -> | ||
{:error, "Error creating chat completion service"} | ||
end | ||
end | ||
|
||
@impl ChatCompletionsService | ||
@spec enabled?() :: boolean() | ||
def enabled? do | ||
ai_config = fetch_ai_config!() | ||
enabled?(ai_config) | ||
end | ||
|
||
def enabled?(ai_config) do | ||
ai_config[:enabled] | ||
end | ||
|
||
defp setup_chat_completions_service do | ||
ai_config = fetch_ai_config!() | ||
|
||
if enabled?(ai_config) do | ||
{:ok, OpenaiEx.new(api_key(ai_config)) |> OpenaiEx.with_base_url(api_base_url(ai_config))} | ||
else | ||
{:error, :ai_not_enabled} | ||
end | ||
end | ||
|
||
defp api_key(ai_config) do | ||
ai_config[:api_key] | ||
end | ||
|
||
defp api_base_url(ai_config) do | ||
ai_config[:api_base_url] | ||
end | ||
|
||
defp fetch_ai_config! do | ||
Application.fetch_env!(:mindwendel, :ai) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Mindwendel.Services.IdeaService do | ||
alias Mindwendel.Services.ChatCompletions.ChatCompletionsService | ||
alias Mindwendel.Ideas | ||
|
||
require Logger | ||
|
||
def idea_generation_enabled? do | ||
ChatCompletionsService.enabled?() | ||
end | ||
|
||
def add_ideas_to_brainstorming(brainstorming) do | ||
if !idea_generation_enabled?() do | ||
[] | ||
else | ||
generated_ideas = ChatCompletionsService.generate_ideas(brainstorming.name) | ||
|
||
Enum.map(generated_ideas, fn generated_idea -> | ||
Ideas.create_idea(%{ | ||
username: "AI", | ||
body: generated_idea["idea"], | ||
brainstorming_id: brainstorming.id | ||
}) | ||
end) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.