You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generation is used as a building block for certain CocoIndex functions that process data using LLM generation.
35
+
36
+
We have one builtin functions using LLM generation for now:
37
+
38
+
*[`ExtractByLlm`](/docs/ops/functions#extractbyllm): it extracts information from input text.
39
+
40
+
#### LLM Spec
41
+
42
+
When calling a CocoIndex function that uses LLM generation, you need to provide a `cocoindex.LlmSpec` dataclass, to configure the LLM you want to use in these functions.
16
43
It has the following fields:
17
44
18
-
*`api_type`: The type of integrated LLM API to use, e.g. `cocoindex.LlmApiType.OPENAI` or `cocoindex.LlmApiType.OLLAMA`.
45
+
*`api_type` (type: [`cocoindex.LlmApiType`](/docs/ai/llm#llm-api-types), required): The type of integrated LLM API to use, e.g. `cocoindex.LlmApiType.OPENAI` or `cocoindex.LlmApiType.OLLAMA`.
19
46
See supported LLM APIs in the [LLM API integrations](#llm-api-integrations) section below.
20
-
*`model`: The name of the LLM model to use.
21
-
*`address` (optional): The address of the LLM API.
47
+
*`model` (type: `str`, required): The name of the LLM model to use.
48
+
*`address` (type: `str`, optional): The address of the LLM API.
22
49
23
50
51
+
### Text Embedding
52
+
53
+
Embedding means converting text into a vector space, usually for similarity matching.
54
+
55
+
We provide a builtin function [`EmbedText`](/docs/ops/functions#embedtext) that converts a given text into a vector space.
See documentation for [`EmbedText`](/docs/ops/functions#embedtext) for more details about these fields.
65
+
24
66
## LLM API Integrations
25
67
26
68
CocoIndex integrates with various LLM APIs for these functions.
@@ -30,7 +72,11 @@ CocoIndex integrates with various LLM APIs for these functions.
30
72
To use the OpenAI LLM API, you need to set the environment variable `OPENAI_API_KEY`.
31
73
You can generate the API key from [OpenAI Dashboard](https://platform.openai.com/api-keys).
32
74
33
-
A spec for OpenAI looks like this:
75
+
Currently we don't support custom address for OpenAI API.
76
+
77
+
You can find the full list of models supported by OpenAI [here](https://platform.openai.com/docs/models).
78
+
79
+
For text generation, a spec for OpenAI looks like this:
34
80
35
81
<Tabs>
36
82
<TabItemvalue="python"label="Python"default>
@@ -42,9 +88,20 @@ cocoindex.LlmSpec(
42
88
)
43
89
```
44
90
45
-
Currently we don't support custom address for OpenAI API.
91
+
</TabItem>
92
+
</Tabs>
46
93
47
-
You can find the full list of models supported by OpenAI [here](https://platform.openai.com/docs/models).
94
+
For text embedding, a spec for OpenAI looks like this:
95
+
96
+
<Tabs>
97
+
<TabItemvalue="python"label="Python"default>
98
+
99
+
```python
100
+
cocoindex.functions.EmbedText(
101
+
api_type=cocoindex.LlmApiType.OPENAI,
102
+
model="text-embedding-3-small",
103
+
)
104
+
```
48
105
49
106
</TabItem>
50
107
</Tabs>
@@ -82,7 +139,9 @@ cocoindex.LlmSpec(
82
139
To use the Gemini LLM API, you need to set the environment variable `GEMINI_API_KEY`.
83
140
You can generate the API key from [Google AI Studio](https://aistudio.google.com/apikey).
84
141
85
-
A spec for Gemini looks like this:
142
+
You can find the full list of models supported by Gemini [here](https://ai.google.dev/gemini-api/docs/models).
143
+
144
+
For text generation, a spec looks like this:
86
145
87
146
<Tabs>
88
147
<TabItemvalue="python"label="Python"default>
@@ -97,14 +156,32 @@ cocoindex.LlmSpec(
97
156
</TabItem>
98
157
</Tabs>
99
158
100
-
You can find the full list of models supported by Gemini [here](https://ai.google.dev/gemini-api/docs/models).
159
+
For text embedding, a spec looks like this:
160
+
161
+
<Tabs>
162
+
<TabItemvalue="python"label="Python"default>
163
+
164
+
```python
165
+
cocoindex.functions.EmbedText(
166
+
api_type=cocoindex.LlmApiType.GEMINI,
167
+
model="text-embedding-004",
168
+
task_type="SEMANTICS_SIMILARITY",
169
+
)
170
+
```
171
+
172
+
All supported embedding models can be found [here](https://ai.google.dev/gemini-api/docs/embeddings#embeddings-models).
173
+
Gemini supports task type (optional), which can be found [here](https://ai.google.dev/gemini-api/docs/embeddings#supported-task-types).
174
+
175
+
176
+
</TabItem>
177
+
</Tabs>
101
178
102
179
### Anthropic
103
180
104
181
To use the Anthropic LLM API, you need to set the environment variable `ANTHROPIC_API_KEY`.
105
182
You can generate the API key from [Anthropic API](https://console.anthropic.com/settings/keys).
106
183
107
-
A spec for Anthropic looks like this:
184
+
A text generation spec for Anthropic looks like this:
108
185
109
186
<Tabs>
110
187
<TabItemvalue="python"label="Python"default>
@@ -121,6 +198,29 @@ cocoindex.LlmSpec(
121
198
122
199
You can find the full list of models supported by Anthropic [here](https://docs.anthropic.com/en/docs/about-claude/models/all-models).
123
200
201
+
### Voyage
202
+
203
+
To use the Voyage LLM API, you need to set the environment variable `VOYAGE_API_KEY`.
204
+
You can generate the API key from [Voyage dashboard](https://dashboard.voyageai.com/organization/api-keys).
205
+
206
+
A text embedding spec for Voyage looks like this:
207
+
208
+
<Tabs>
209
+
<TabItemvalue="python"label="Python"default>
210
+
211
+
```python
212
+
cocoindex.functions.EmbedText(
213
+
api_type=cocoindex.LlmApiType.VOYAGE,
214
+
model="voyage-code-3",
215
+
task_type="document",
216
+
)
217
+
```
218
+
219
+
</TabItem>
220
+
</Tabs>
221
+
222
+
Voyage API supports `document` and `query` as task types (optional, a.k.a. `input_type` in Voyage API, see [Voyage API documentation](https://docs.voyageai.com/reference/embeddings-api) for details).
223
+
124
224
### LiteLLM
125
225
126
226
To use the LiteLLM API, you need to set the environment variable `LITELLM_API_KEY`.
Copy file name to clipboardExpand all lines: docs/docs/ops/functions.md
+29Lines changed: 29 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -105,3 +105,32 @@ Input data:
105
105
*`text` (type: `str`, required): The text to extract information from.
106
106
107
107
Return type: As specified by the `output_type` field in the spec. The extracted information from the input text.
108
+
109
+
## EmbedText
110
+
111
+
`EmbedText` embeds a text into a vector space using various LLM APIs that support text embedding.
112
+
113
+
The spec takes the following fields:
114
+
115
+
*`api_type` (type: [`cocoindex.LlmApiType`](/docs/ai/llm#llm-api-types), required): The type of LLM API to use for embedding.
116
+
*`model` (type: `str`, required): The name of the embedding model to use.
117
+
*`address` (type: `str`, optional): The address of the LLM API. If not specified, uses the default address for the API type.
118
+
*`output_dimension` (type: `int`, optional): The expected dimension of the output embedding vector. If not specified, use the default dimension of the model.
119
+
120
+
For most API types, the function internally keeps a registry for the default output dimension of known model.
121
+
You need to explicitly specify the `output_dimension` if you want to use a new model that is not in the registry yet.
122
+
123
+
*`task_type` (type: `str`, optional): The task type for embedding, used by some embedding models to optimize the embedding for specific use cases.
124
+
125
+
:::note Supported APIs for Text Embedding
126
+
127
+
Not all LLM APIs support text embedding. See the [LLM API Types table](/docs/ai/llm#llm-api-types) for which APIs support text embedding functionality.
128
+
129
+
:::
130
+
131
+
Input data:
132
+
133
+
*`text` (type: `str`, required): The text to embed.
134
+
135
+
Return type: `vector[float32; N]`, where `N` is the dimension of the embedding vector determined by the model.
0 commit comments