-
Notifications
You must be signed in to change notification settings - Fork 19
Create/update AI Configuration #4147
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| defmodule TrentoWeb.V1.AIConfigurationController do | ||
| use TrentoWeb, :controller | ||
| use OpenApiSpex.ControllerSpecs | ||
|
|
||
| alias Trento.Users.User | ||
|
|
||
| alias Trento.AI | ||
|
|
||
| alias Trento.AI.UserConfiguration | ||
|
|
||
| alias TrentoWeb.OpenApi.V1.Schema | ||
|
|
||
| import Plug.Conn | ||
|
|
||
| plug TrentoWeb.Plugs.LoadUserPlug | ||
|
|
||
| plug OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true | ||
| action_fallback TrentoWeb.FallbackController | ||
|
|
||
| operation :create_ai_configuration, | ||
| summary: "Creates User's AI Configuration", | ||
| description: "Creates a new AI configuration for the currently authenticated user.", | ||
| tags: ["Profile"], | ||
| request_body: | ||
| {"CreateUserAIConfiguration", "application/json", Schema.AI.CreateUserConfigurationRequest}, | ||
| responses: [ | ||
| created: | ||
| {"User AI Configuration created successfully.", "application/json", | ||
| Schema.AI.UserConfiguration}, | ||
| unprocessable_entity: Schema.UnprocessableEntity.response(), | ||
| unauthorized: Schema.Unauthorized.response(), | ||
| forbidden: Schema.Forbidden.response() | ||
| ] | ||
|
|
||
| def create_ai_configuration(conn, _) do | ||
| %User{} = current_user = Pow.Plug.current_user(conn) | ||
|
|
||
| creation_params = OpenApiSpex.body_params(conn) | ||
|
|
||
| with {:ok, %UserConfiguration{} = user_ai_config} <- | ||
| AI.create_user_configuration(current_user, creation_params) do | ||
| conn | ||
| |> put_status(:created) | ||
| |> render(:ai_configuration, %{ai_configuration: user_ai_config}) | ||
| end | ||
| end | ||
|
|
||
| operation :update_ai_configuration, | ||
| summary: "Updates User's AI Configuration", | ||
| description: "Updates the AI configuration for the currently authenticated user.", | ||
| tags: ["Profile"], | ||
| request_body: | ||
| {"UpdateUserAIConfiguration", "application/json", Schema.AI.UpdateUserConfigurationRequest}, | ||
| responses: [ | ||
| ok: | ||
| {"User AI Configuration updated successfully.", "application/json", | ||
| Schema.AI.UserConfiguration}, | ||
| unprocessable_entity: Schema.UnprocessableEntity.response(), | ||
| unauthorized: Schema.Unauthorized.response(), | ||
| forbidden: Schema.Forbidden.response() | ||
| ] | ||
|
|
||
| def update_ai_configuration(conn, _) do | ||
| %User{} = current_user = Pow.Plug.current_user(conn) | ||
|
|
||
| update_params = OpenApiSpex.body_params(conn) | ||
|
|
||
| with {:ok, %UserConfiguration{} = user_ai_config} <- | ||
| AI.update_user_configuration(current_user, update_params) do | ||
| conn | ||
| |> put_status(:ok) | ||
| |> render(:ai_configuration, %{ai_configuration: user_ai_config}) | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| defmodule TrentoWeb.V1.AIConfigurationJSON do | ||
| def ai_configuration(%{ai_configuration: ai_configuration}), | ||
| do: ai_configuration_entry(ai_configuration) | ||
|
|
||
| def ai_configuration_entry(%{ | ||
| provider: provider, | ||
| model: model | ||
| }), | ||
| do: %{ | ||
| provider: provider, | ||
| model: model | ||
| } | ||
|
|
||
| def ai_configuration_entry(_), do: nil | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| defmodule TrentoWeb.OpenApi.V1.Schema.AI do | ||
| @moduledoc false | ||
|
|
||
| require OpenApiSpex | ||
| alias OpenApiSpex.Schema | ||
|
|
||
| defmodule UserConfiguration do | ||
| @moduledoc false | ||
|
|
||
| OpenApiSpex.schema( | ||
nelsonkopliku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| %{ | ||
| title: "AIUserConfigurationV1", | ||
| description: "AI configuration for a user.", | ||
| type: :object, | ||
| nullable: true, | ||
| additionalProperties: false, | ||
| properties: %{ | ||
| provider: %Schema{ | ||
| type: :string, | ||
| description: "Chosen AI provider.", | ||
| example: "googleai", | ||
| nullable: false | ||
| }, | ||
| model: %Schema{ | ||
| type: :string, | ||
| description: "Chosen AI model.", | ||
| example: "gemini-2.0-flash", | ||
| nullable: false | ||
| } | ||
| }, | ||
| example: %{ | ||
| provider: "googleai", | ||
| model: "gemini-2.0-flash" | ||
| }, | ||
| required: [:provider, :model] | ||
| }, | ||
| struct?: false | ||
| ) | ||
| end | ||
|
|
||
| defmodule CreateUserConfigurationRequest do | ||
| @moduledoc false | ||
|
|
||
| OpenApiSpex.schema( | ||
| %{ | ||
| title: "CreateUserConfigurationRequestV1", | ||
| description: "AI configuration request for a user.", | ||
| type: :object, | ||
| additionalProperties: false, | ||
| properties: %{ | ||
| provider: %Schema{ | ||
| type: :string, | ||
| description: "AI provider.", | ||
| nullable: false, | ||
| example: "googleai" | ||
| }, | ||
| model: %Schema{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is to extract the provider based on the model. See here
Good question. Technically it might be possible, although it's not in the immediate scope. Anyway, in the current scheme of things where the provider/model map is defined as follows config :trento, :ai,
enabled: true,
providers: [
googleai: [
models: [
"gemini-2.5-pro",
"gemini-2.5-flash",
"gemini-2.5-flash-lite",
"gemini-3.1-flash-preview",
"gemini-3.1-flash-lite-preview",
"gemini-3.1-pro-preview"
]
],
openai: [
models: [
"o3-mini",
"o3",
"gpt-4.1",
"gpt-4",
"gpt-5-mini",
"gpt-5.4"
]
],
anthropic: [
models: [
"claude-opus-4-6",
"claude-sonnet-4-6",
"claude-haiku-4-5"
]
]
]we could have the same model in many providers by for instace prepending the provider to the model name This could be either at config level or extracted when provided as a request parameter. I think we can extend when the need arises.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I made It was a bit more tedious than I hoped and made the PR changes rise by more than 50% 🙈 (it is fair to say that it is mainly tests, tho) Now, in total honesty I found the previous version simpler, and it was easily extendable as well to support the same model over many providers scenario by changing the pattern of the model name (the However, in any case it is very likely that things around this topic will change because we aim to be able to get the model list from providers' APIs, but we're not sure it will be in the very first version. If you ask me, I would revert the last commit 700aa78 and go ahead from there, but I want to hear your opinion as well. |
||
| type: :string, | ||
| description: "AI model.", | ||
| nullable: false, | ||
| example: "gemini-2.0-flash" | ||
| }, | ||
| api_key: %Schema{ | ||
| type: :string, | ||
| description: "AI API key.", | ||
| nullable: false, | ||
| example: "AIza..." | ||
| } | ||
| }, | ||
| example: %{ | ||
| provider: "googleai", | ||
| model: "gemini-2.0-flash", | ||
| api_key: "AIza..." | ||
| }, | ||
| required: [:provider, :model, :api_key] | ||
| }, | ||
| struct?: false | ||
| ) | ||
| end | ||
|
|
||
| defmodule UpdateUserConfigurationRequest do | ||
| @moduledoc false | ||
|
|
||
| OpenApiSpex.schema( | ||
| %{ | ||
| title: "UpdateUserConfigurationRequestV1", | ||
| description: "AI configuration request for a user.", | ||
| type: :object, | ||
| additionalProperties: false, | ||
| minProperties: 1, | ||
| properties: %{ | ||
| provider: %Schema{ | ||
| type: :string, | ||
| description: "AI provider.", | ||
| nullable: false, | ||
| example: "googleai" | ||
| }, | ||
| model: %Schema{ | ||
| type: :string, | ||
| description: "AI model.", | ||
| nullable: false, | ||
| example: "gemini-2.0-flash" | ||
| }, | ||
| api_key: %Schema{ | ||
| type: :string, | ||
| description: "AI API key.", | ||
| nullable: false, | ||
| example: "AIza..." | ||
| } | ||
| }, | ||
| example: %{ | ||
| api_key: "AIza...", | ||
| model: "gemini-2.0-flash" | ||
| } | ||
| }, | ||
| struct?: false | ||
| ) | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.