adapt Jina Embeddings to new Jina AI Embedding API (#13658)

- **Description:** Adapt JinaEmbeddings to run with the new Jina AI
Embedding platform
- **Twitter handle:** https://twitter.com/JinaAI_

---------

Co-authored-by: Joan Fontanals Martinez <joan.fontanals.martinez@jina.ai>
Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
This commit is contained in:
Joan Fontanals
2023-12-05 05:40:33 +01:00
committed by GitHub
parent e0c03d6c44
commit dcccf8fa66
3 changed files with 63 additions and 138 deletions

View File

@@ -1,75 +1,20 @@
# Jina
This page covers how to use the Jina ecosystem within LangChain.
This page covers how to use the Jina Embeddings within LangChain.
It is broken into two parts: installation and setup, and then references to specific Jina wrappers.
## Installation and Setup
- Install the Python SDK with `pip install jina`
- Get a Jina AI Cloud auth token from [here](https://cloud.jina.ai/settings/tokens) and set it as an environment variable (`JINA_AUTH_TOKEN`)
## Wrappers
### Embeddings
- Get a Jina AI API token from [here](https://jina.ai/embeddings/) and set it as an environment variable (`JINA_API_TOKEN`)
There exists a Jina Embeddings wrapper, which you can access with
```python
from langchain.embeddings import JinaEmbeddings
```
For a more detailed walkthrough of this, see [this notebook](/docs/integrations/text_embedding/jina)
## Deployment
[Langchain-serve](https://github.com/jina-ai/langchain-serve), powered by Jina, helps take LangChain apps to production with easy to use REST/WebSocket APIs and Slack bots.
### Usage
Install the package from PyPI.
```bash
pip install langchain-serve
# you can pas jina_api_key, if none is passed it will be taken from `JINA_API_TOKEN` environment variable
embeddings = JinaEmbeddings(jina_api_key='jina_**', model_name='jina-embeddings-v2-base-en')
```
Wrap your LangChain app with the `@serving` decorator.
You can check the list of available models from [here](https://jina.ai/embeddings/)
```python
# app.py
from lcserve import serving
@serving
def ask(input: str) -> str:
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from langchain.agents import AgentExecutor, ZeroShotAgent
tools = [...] # list of tools
prompt = ZeroShotAgent.create_prompt(
tools, input_variables=["input", "agent_scratchpad"],
)
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
agent = ZeroShotAgent(
llm_chain=llm_chain, allowed_tools=[tool.name for tool in tools]
)
agent_executor = AgentExecutor.from_agent_and_tools(
agent=agent,
tools=tools,
verbose=True,
)
return agent_executor.run(input)
```
Deploy on Jina AI Cloud with `lc-serve deploy jcloud app`. Once deployed, we can send a POST request to the API endpoint to get a response.
```bash
curl -X 'POST' 'https://<your-app>.wolf.jina.ai/ask' \
-d '{
"input": "Your Question here?",
"envs": {
"OPENAI_API_KEY": "sk-***"
}
}'
```
You can also self-host the app on your infrastructure with Docker-compose or Kubernetes. See [here](https://github.com/jina-ai/langchain-serve#-self-host-llm-apps-with-docker-compose-or-kubernetes) for more details.
Langchain-serve also allows to deploy the apps with WebSocket APIs and Slack Bots both on [Jina AI Cloud](https://cloud.jina.ai/) or self-hosted infrastructure.
For a more detailed walkthrough of this, see [this notebook](/docs/integrations/text_embedding/jina.ipynb)

View File

@@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "d94c62b4",
"metadata": {},
"outputs": [],
@@ -28,7 +28,7 @@
"outputs": [],
"source": [
"embeddings = JinaEmbeddings(\n",
" jina_auth_token=jina_auth_token, model_name=\"ViT-B-32::openai\"\n",
" jina_api_key=\"jina_*\", model_name=\"jina-embeddings-v2-base-en\"\n",
")"
]
},
@@ -52,6 +52,16 @@
"query_result = embeddings.embed_query(text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aea3ca33-1e6e-499c-8284-b7e26f38c514",
"metadata": {},
"outputs": [],
"source": [
"print(query_result)"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -62,21 +72,15 @@
"doc_result = embeddings.embed_documents([text])"
]
},
{
"cell_type": "markdown",
"id": "6f3607a0",
"metadata": {},
"source": [
"In the above example, `ViT-B-32::openai`, OpenAI's pretrained `ViT-B-32` model is used. For a full list of models, see [here](https://cloud.jina.ai/user/inference/model/63dca9df5a0da83009d519cd)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd5f148e",
"id": "c2e6b743-768c-4d7e-a331-27d5f0e8e30e",
"metadata": {},
"outputs": [],
"source": []
"source": [
"print(doc_result)"
]
}
],
"metadata": {
@@ -95,7 +99,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.10.11"
}
},
"nbformat": 4,