-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Support embeddings models #3252
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
base: main
Are you sure you want to change the base?
Conversation
| from pydantic_ai.models.instrumented import InstrumentationSettings | ||
| from pydantic_ai.providers import infer_provider | ||
|
|
||
| KnownEmbeddingModelName = TypeAliasType( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test like this one to verify this is up to date:
| def test_known_model_names(): # pragma: lax no cover |
|
|
||
| return CohereEmbeddingModel(model_name, provider=provider) | ||
| else: | ||
| raise UserError(f'Unknown embeddings model: {model}') # pragma: no cover |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/ggozad/haiku.rag/tree/main/src/haiku/rag/embeddings has Ollama, vLLM and VoyageAI, which would be worth adding as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/ggozad/haiku.rag/tree/main/src/haiku/rag/embeddings has Ollama, vLLM and VoyageAI, which would be worth adding as well
Nice.
For people doing local development OLlama and LlamaCPP (if you don't have it already) are essentials.
Docs Preview
|
|
Thanks for starting this and please do let me know if you need help :) One thing you might want to support from the start is having as part of the Embedding models have a limit of how many tokens of input they can handle. Most providers will raise ( All this is well explained here I would not necessarily truncate like in the cookbook and still just raise, but I would be grateful to have available from the model side the The only difficulty I see with this is that not all providers expose the tokenizers, for example Ollama does not. But still, would be nice to have it for the providers that do support it, as it's a crucial step when you are trying to chunk a document for embedding. In Edit: I am not suggesting that calling |
|
|
||
| def __init__( | ||
| self, | ||
| model_name: OpenAIEmbeddingModelName, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should also be in Model - We don't have it right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kludex What about models that don't take a model name, like TestModel, FunctionModel, WrapperModel?
gvanrossum
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to be able to comment on the API, but there are no tests showing how to call it.
|
@gvanrossum I'll make some progress on the PR today, but this is the API as it stands today: import asyncio
from pydantic_ai.embeddings import Embedder
embedder = Embedder("openai:text-embedding-3-large")
async def main():
result = await embedder.embed("Hello, world!")
print(result)
if __name__ == "__main__":
asyncio.run(main())With Azure OpenAI you currently have to create the model and provider manually, but we'll make import asyncio
from pydantic_ai.embeddings import Embedder
from pydantic_ai.embeddings.openai import OpenAIEmbeddingModel
from pydantic_ai.providers.azure import AzureProvider
model = OpenAIEmbeddingModel("text-embedding-3-large", provider=AzureProvider())
embedder = Embedder(model)
async def main():
result = await embedder.embed("Hello, world!")
print(result)
if __name__ == "__main__":
asyncio.run(main()) |
|
Nice. Do you have a bulk API too? That's essential for typeagent. |
|
@gvanrossum Yep, the |
|
@gvanrossum In case you'd like to give it a try pre-release, I've made some progress today, including support for |
|
Unfortunately I haven't managed to get to this this week. Next week should be better. |
# Conflicts: # pydantic_ai_slim/pydantic_ai/models/__init__.py
|
@ggozad You'll be happy to hear that |
I have been following along the PR and I just can't wait for a release! This looks really good, looking forward to removing a bunch of my code :) |
|
Following this PR now!
|
|
It might be nice to be able to do this as a single function call so you don't always need to create the embedder ahead of time, but I'm not sure if this fits with the rest of pydantic.ai ? |
|
@stuartaxonHO I personally don't think it's worth adding a helper function when the "verbose" version is just |
|
Think I was spoiled by the litellm version |
Started this in collaboration with @DouweM, I'd like to ensure consensus on the API design before adding the remaining-providers/logfire-instrumentation/docs/tests.
This is inspired by the approach in haiku.rag, though we adapted it to be a bit closer to the
AgentAPIs are used (and how you can override model, settings, etc.).Closes #58
Example:
To do:
Embedder.embed_synccount_tokensmax_input_tokenslogfire.instrument_pydantic_ai()ModelAPIError)