Skip to content

Commit

Permalink
refactor: make it easier to set adapter schema
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Apr 6, 2024
1 parent 5089082 commit fe0e369
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ require("codecompanion").setup({
})
```

> [!TIP]
> To create your own adapter please refer to the [ADAPTERS](ADAPTERS.md) guide.
#### Configuring environment variables

You can customise an adapter's configuration as follows:

```lua
Expand All @@ -205,19 +210,38 @@ In the example above, we've changed the name of the default API key which the An
```lua
require("codecompanion").setup({
adapters = {
chat = require("codecompanion.adapters").use("openai", {
openai = require("codecompanion.adapters").use("openai", {
env = {
api_key = "cmd:gpg --decrypt ~/.openai-api-key.gpg 2>/dev/null",
api_key = "cmd:op read op://personal/OpenAI/credential --no-newline",
},
}),
strategies = {
chat = "openai",
inline = "anthropic"
},
},
})
```

In this example, we're using `gpg` to decrypt a file to obtain an API key for OpenAI.
In this example, we're using the 1Password CLI to read an OpenAI credential.

> [!TIP]
> To create your own adapter please refer to the [ADAPTERS](ADAPTERS.md) guide.
#### Configuring adapter settings

Generative AI services have many settings such as _model_, _temperature_ and _max_tokens_. In an adapter, these sit within a schema table and can be configured during setup:

```lua
require("codecompanion").setup({
adapters = {
anthropic = require("codecompanion.adapters").use("anthropic", {
schema = {
model = {
default = "claude-3-sonnet-20240229",
},
},
}),
},
})
```

### Edgy.nvim Configuration

Expand Down
41 changes: 34 additions & 7 deletions doc/codecompanion.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*codecompanion.txt* For NVIM v0.9.2 Last change: 2024 April 04
*codecompanion.txt* For NVIM v0.9.2 Last change: 2024 April 06

==============================================================================
Table of Contents *codecompanion-table-of-contents*
Expand Down Expand Up @@ -165,6 +165,12 @@ strategy:
})
<


[!TIP] To create your own adapter please refer to the ADAPTERS <ADAPTERS.md>
guide.

CONFIGURING ENVIRONMENT VARIABLES

You can customise an adapter’s configuration as follows:

>lua
Expand Down Expand Up @@ -192,21 +198,42 @@ from within the configuration:
>lua
require("codecompanion").setup({
adapters = {
chat = require("codecompanion.adapters").use("openai", {
openai = require("codecompanion.adapters").use("openai", {
env = {
api_key = "cmd:gpg --decrypt ~/.openai-api-key.gpg 2>/dev/null",
api_key = "cmd:op read op://personal/OpenAI/credential --no-newline",
},
}),
strategies = {
chat = "openai",
inline = "anthropic"
},
},
})
<

In this example, we’re using `gpg` to decrypt a file to obtain an API key for
OpenAI.
In this example, we’re using the 1Password CLI to read an OpenAI credential.


[!TIP] To create your own adapter please refer to the ADAPTERS <ADAPTERS.md>
guide.
CONFIGURING ADAPTER SETTINGS

Generative AI services have many settings such as _model_, _temperature_ and
_max_tokens_. In an adapter, these sit within a schema table and can be
configured during setup:

>lua
require("codecompanion").setup({
adapters = {
anthropic = require("codecompanion.adapters").use("anthropic", {
schema = {
model = {
default = "claude-3-sonnet-20240229",
},
},
}),
},
})
<


EDGY.NVIM CONFIGURATION ~

Expand Down
4 changes: 4 additions & 0 deletions lua/codecompanion/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ M.setup = function(opts)
if opts and opts.adapters then
for name, adapter in pairs(opts.adapters) do
if M.options.adapters[name] then
if adapter.schema then
M.options.adapters[name].schema =
vim.tbl_deep_extend("force", M.options.adapters[name].schema, adapter.schema)
end
M.options.adapters[name] = adapter
end
end
Expand Down

0 comments on commit fe0e369

Please sign in to comment.