Skip to content

Commit bafccef

Browse files
Add examples of using Fields and EnvAuthStrategy to developer documentation (#1056)
* Add example of using fields for configuration. I have created this example based on some delving into the code and looking at examples of some of the community providers. I haven't included the IntegerField, as I couldn't get it to work properly, suggesting that I don't understand it properly, whereas I have successfully tested the TextField and MultilineTextField. * Add example of using the EnvAuthStrategy to provide API Keys * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Combined sections for API Keys and for Fields into a single section. As per review feedback, combined the two sections and examples for brevity. Also added a couple of missing imports to the example. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ec34146 commit bafccef

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

docs/source/developers/index.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,61 @@ your new provider's `id`:
121121
[LLM]: https://api.python.langchain.com/en/v0.0.339/llms/langchain.llms.base.LLM.html#langchain.llms.base.LLM
122122
[BaseChatModel]: https://api.python.langchain.com/en/v0.0.339/chat_models/langchain.chat_models.base.BaseChatModel.html
123123

124+
### API keys and fields for custom providers
125+
126+
You can add handle authentication via API keys, and configuration with
127+
custom parameters using an auth strategy and fields as shown in the example
128+
below.
129+
130+
```python
131+
from typing import ClassVar, List
132+
from jupyter_ai_magics import BaseProvider
133+
from jupyter_ai_magics.providers import EnvAuthStrategy, Field, TextField, MultilineTextField
134+
from langchain_community.llms import FakeListLLM
135+
136+
137+
class MyProvider(BaseProvider, FakeListLLM):
138+
id = "my_provider"
139+
name = "My Provider"
140+
model_id_key = "model"
141+
models = [
142+
"model_a",
143+
"model_b"
144+
]
145+
146+
auth_strategy = EnvAuthStrategy(
147+
name="MY_API_KEY", keyword_param="my_api_key_param"
148+
)
149+
150+
fields: ClassVar[List[Field]] = [
151+
TextField(key="my_llm_parameter", label="The name for my_llm_parameter to show in the UI"),
152+
MultilineTextField(key="custom_config", label="Custom Json Config", format="json"),
153+
]
154+
155+
def __init__(self, **kwargs):
156+
model = kwargs.get("model_id")
157+
kwargs["responses"] = (
158+
["This is a response from model 'a'"]
159+
if model == "model_a" else
160+
["This is a response from model 'b'"]
161+
)
162+
super().__init__(**kwargs)
163+
```
164+
165+
The `auth_strategy` handles specifying API keys for providers and models.
166+
The example shows the `EnvAuthStrategy` which takes the API key from the
167+
environment variable with the name specified in `name` and be provided to the
168+
model's `__init__` as a kwarg with the name specified in `keyword_param`.
169+
This will also cause a field to be present in the configuration UI with the
170+
`name` of the environment variable as the label.
171+
172+
Further configuration can be handled adding `fields` into the settings
173+
dialogue for your custom model by specifying a list of fields as shown in
174+
the example. These will be passed into the `__init__` as kwargs, with the
175+
key specified by the key in the field object. The label specified in the field
176+
object determines the text shown in the configuration section of the user
177+
interface.
178+
124179
### Custom embeddings providers
125180

126181
To provide a custom embeddings model an embeddings providers should be defined implementing the API of `jupyter-ai`'s `BaseEmbeddingsProvider` and of `langchain`'s [`Embeddings`][Embeddings] abstract class.

0 commit comments

Comments
 (0)