Skip to content

Implement Gitlab::Llm::Anthropic::Client

Patrick Bajao requested to merge 409812-anthropic-client into master

What does this MR do and why?

This implements a client class that can be used in the future for interacting with Anthropic API.

Sample usage:

Gitlab::Llm::Anthropic::Client.new(user).complete(prompt: 'Hi')

Migration

db:migrate

main: == [advisory_lock_connection] object_id: 313520, pg_backend_pid: 1706
main: == 20230504112809 AddAnthropicApiKeyApplicationSetting: migrating =============
main: -- add_column(:application_settings, :encrypted_anthropic_api_key, :binary)
main:    -> 0.0019s
main: -- add_column(:application_settings, :encrypted_anthropic_api_key_iv, :binary)
main:    -> 0.0006s
main: == 20230504112809 AddAnthropicApiKeyApplicationSetting: migrated (0.0062s) ====

main: == [advisory_lock_connection] object_id: 313520, pg_backend_pid: 1706
ci: == [advisory_lock_connection] object_id: 313760, pg_backend_pid: 1708
ci: == 20230504112809 AddAnthropicApiKeyApplicationSetting: migrating =============
ci: -- add_column(:application_settings, :encrypted_anthropic_api_key, :binary)
ci:    -> 0.0012s
ci: -- add_column(:application_settings, :encrypted_anthropic_api_key_iv, :binary)
ci:    -> 0.0007s
ci: == 20230504112809 AddAnthropicApiKeyApplicationSetting: migrated (0.0086s) ====

ci: == [advisory_lock_connection] object_id: 313760, pg_backend_pid: 1708

db:rollback

main: == [advisory_lock_connection] object_id: 313240, pg_backend_pid: 612
main: == 20230504112809 AddAnthropicApiKeyApplicationSetting: reverting =============
main: -- remove_column(:application_settings, :encrypted_anthropic_api_key_iv, :binary)
main:    -> 0.0024s
main: -- remove_column(:application_settings, :encrypted_anthropic_api_key, :binary)
main:    -> 0.0005s
main: == 20230504112809 AddAnthropicApiKeyApplicationSetting: reverted (0.0081s) ====

main: == [advisory_lock_connection] object_id: 313240, pg_backend_pid: 612
ci: == [advisory_lock_connection] object_id: 313260, pg_backend_pid: 1301
ci: == 20230504112809 AddAnthropicApiKeyApplicationSetting: reverting =============
ci: -- remove_column(:application_settings, :encrypted_anthropic_api_key_iv, :binary)
ci:    -> 0.0025s
ci: -- remove_column(:application_settings, :encrypted_anthropic_api_key, :binary)
ci:    -> 0.0007s
ci: == 20230504112809 AddAnthropicApiKeyApplicationSetting: reverted (0.0115s) ====

ci: == [advisory_lock_connection] object_id: 313260, pg_backend_pid: 1301

How to set up and validate locally

  1. Set your API key via rails console: Gitlab::CurrentSettings.update!(anthropic_api_key: <insert API key>). If you don't have one yet, create one in https://console.anthropic.com/account/keys if you have access.
  2. Make a sample request and it should be successful:
Gitlab::Llm::Anthropic::Client.new(user).complete(prompt: 'Hi')

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #409812

Edited by Patrick Bajao

Merge request reports