forked from phoenix/litellm-mirror
144 lines
3.3 KiB
Text
144 lines
3.3 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Xorbits inference (Xinference)\n",
|
|
"\n",
|
|
"This notebook goes over how to use Xinference embeddings within LangChain"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Installation\n",
|
|
"\n",
|
|
"Install `Xinference` through PyPI:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%pip install \"xinference[all]\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Deploy Xinference Locally or in a Distributed Cluster.\n",
|
|
"\n",
|
|
"For local deployment, run `xinference`. \n",
|
|
"\n",
|
|
"To deploy Xinference in a cluster, first start an Xinference supervisor using the `xinference-supervisor`. You can also use the option -p to specify the port and -H to specify the host. The default port is 9997.\n",
|
|
"\n",
|
|
"Then, start the Xinference workers using `xinference-worker` on each server you want to run them on. \n",
|
|
"\n",
|
|
"You can consult the README file from [Xinference](https://github.com/xorbitsai/inference) for more information.\n",
|
|
"\n",
|
|
"## Wrapper\n",
|
|
"\n",
|
|
"To use Xinference with LangChain, you need to first launch a model. You can use command line interface (CLI) to do so:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Model uid: 915845ee-2a04-11ee-8ed4-d29396a3f064\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"!xinference launch -n vicuna-v1.3 -f ggmlv3 -q q4_0"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"A model UID is returned for you to use. Now you can use Xinference embeddings with LangChain:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.embeddings import XinferenceEmbeddings\n",
|
|
"\n",
|
|
"xinference = XinferenceEmbeddings(\n",
|
|
" server_url=\"http://0.0.0.0:9997\",\n",
|
|
" model_uid = \"915845ee-2a04-11ee-8ed4-d29396a3f064\"\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"query_result = xinference.embed_query(\"This is a test query\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"doc_result = xinference.embed_documents([\"text A\", \"text B\"])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Lastly, terminate the model when you do not need to use it:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!xinference terminate --model-uid \"915845ee-2a04-11ee-8ed4-d29396a3f064\""
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "base",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.11"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|