From 5f1b5aa6e2a1dcb7ef0daff8e602f933b02b3b2e Mon Sep 17 00:00:00 2001 From: Miri Bar Date: Mon, 11 Nov 2024 14:07:35 +0200 Subject: [PATCH] docs: update ai21 docs --- docs/docs/integrations/llms/ai21.ipynb | 221 -------------- docs/docs/integrations/providers/ai21.mdx | 43 +-- .../integrations/text_embedding/ai21.ipynb | 270 ------------------ 3 files changed, 1 insertion(+), 533 deletions(-) delete mode 100644 docs/docs/integrations/llms/ai21.ipynb delete mode 100644 docs/docs/integrations/text_embedding/ai21.ipynb diff --git a/docs/docs/integrations/llms/ai21.ipynb b/docs/docs/integrations/llms/ai21.ipynb deleted file mode 100644 index cf83e033e0dd5..0000000000000 --- a/docs/docs/integrations/llms/ai21.ipynb +++ /dev/null @@ -1,221 +0,0 @@ -{ - "cells": [ - { - "cell_type": "raw", - "id": "602a52a4", - "metadata": {}, - "source": [ - "---\n", - "sidebar_label: AI21 Labs\n", - "---" - ] - }, - { - "cell_type": "markdown", - "id": "9597802c", - "metadata": {}, - "source": [ - "# AI21LLM\n", - "\n", - "This example goes over how to use LangChain to interact with `AI21` Jurassic models. To use the Jamba model, use the [ChatAI21 object](https://python.langchain.com/docs/integrations/chat/ai21/) instead.\n", - "\n", - "[See a full list of AI21 models and tools on LangChain.](https://pypi.org/project/langchain-ai21/)\n", - "\n", - "## Installation" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "59c710c4", - "metadata": { - "ExecuteTime": { - "end_time": "2024-03-05T20:58:42.397591Z", - "start_time": "2024-03-05T20:58:40.944729Z" - } - }, - "outputs": [], - "source": [ - "!pip install -qU langchain-ai21" - ] - }, - { - "cell_type": "markdown", - "id": "560a2f9254963fd7", - "metadata": { - "collapsed": false - }, - "source": [ - "## Environment Setup\n", - "\n", - "We'll need to get a [AI21 API key](https://docs.ai21.com/) and set the `AI21_API_KEY` environment variable:" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "035dea0f", - "metadata": { - "ExecuteTime": { - "end_time": "2024-03-05T20:58:44.465443Z", - "start_time": "2024-03-05T20:58:42.399724Z" - }, - "tags": [] - }, - "outputs": [], - "source": [ - "import os\n", - "from getpass import getpass\n", - "\n", - "if \"AI21_API_KEY\" not in os.environ:\n", - " os.environ[\"AI21_API_KEY\"] = getpass()" - ] - }, - { - "cell_type": "markdown", - "id": "1891df96eb076e1a", - "metadata": { - "collapsed": false - }, - "source": [ - "## Usage" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "98f70927a87e4745", - "metadata": { - "ExecuteTime": { - "end_time": "2024-03-05T20:58:45.859265Z", - "start_time": "2024-03-05T20:58:44.466637Z" - }, - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'\\nLangChain is a (database)\\nLangChain is a database for storing and processing documents'" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from langchain_ai21 import AI21LLM\n", - "from langchain_core.prompts import PromptTemplate\n", - "\n", - "template = \"\"\"Question: {question}\n", - "\n", - "Answer: Let's think step by step.\"\"\"\n", - "\n", - "prompt = PromptTemplate.from_template(template)\n", - "\n", - "model = AI21LLM(model=\"j2-ultra\")\n", - "\n", - "chain = prompt | model\n", - "\n", - "chain.invoke({\"question\": \"What is LangChain?\"})" - ] - }, - { - "cell_type": "markdown", - "id": "9965c10269159ed1", - "metadata": { - "collapsed": false - }, - "source": [ - "# AI21 Contextual Answer\n", - "\n", - "You can use AI21's contextual answers model to receives text or document, serving as a context,\n", - "and a question and returns an answer based entirely on this context.\n", - "\n", - "This means that if the answer to your question is not in the document,\n", - "the model will indicate it (instead of providing a false answer)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "411adf42eab80829", - "metadata": { - "ExecuteTime": { - "end_time": "2024-03-05T20:59:00.943426Z", - "start_time": "2024-03-05T20:59:00.263497Z" - }, - "collapsed": false - }, - "outputs": [], - "source": [ - "from langchain_ai21 import AI21ContextualAnswers\n", - "\n", - "tsm = AI21ContextualAnswers()\n", - "\n", - "response = tsm.invoke(input={\"context\": \"Your context\", \"question\": \"Your question\"})" - ] - }, - { - "cell_type": "markdown", - "id": "af59ffdbf4964875", - "metadata": { - "collapsed": false - }, - "source": [ - "You can also use it with chains and output parsers and vector DBs" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "bc63830f921b4ac9", - "metadata": { - "ExecuteTime": { - "end_time": "2024-03-05T20:59:07.719225Z", - "start_time": "2024-03-05T20:59:07.102950Z" - }, - "collapsed": false - }, - "outputs": [], - "source": [ - "from langchain_ai21 import AI21ContextualAnswers\n", - "from langchain_core.output_parsers import StrOutputParser\n", - "\n", - "tsm = AI21ContextualAnswers()\n", - "chain = tsm | StrOutputParser()\n", - "\n", - "response = chain.invoke(\n", - " {\"context\": \"Your context\", \"question\": \"Your question\"},\n", - ")" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3.11.1 64-bit", - "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.11.4" - }, - "vscode": { - "interpreter": { - "hash": "e971737741ff4ec9aff7dc6155a1060a59a8a6d52c757dbbe66bf8ee389494b1" - } - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/docs/docs/integrations/providers/ai21.mdx b/docs/docs/integrations/providers/ai21.mdx index 60a925363b1fa..fff4dcc3e64cb 100644 --- a/docs/docs/integrations/providers/ai21.mdx +++ b/docs/docs/integrations/providers/ai21.mdx @@ -15,26 +15,6 @@ This page covers how to use the `AI21` ecosystem within `LangChain`. pip install langchain-ai21 ``` -## LLMs - -See a [usage example](/docs/integrations/llms/ai21). - -### AI21 LLM - -```python -from langchain_ai21 import AI21LLM -``` - -### AI21 Contextual Answer - -You can use AI21’s contextual answers model to receive text or document, -serving as a context, and a question and return an answer based entirely on this context. - -```python -from langchain_ai21 import AI21ContextualAnswers -``` - - ## Chat models ### AI21 Chat @@ -43,25 +23,4 @@ See a [usage example](/docs/integrations/chat/ai21). ```python from langchain_ai21 import ChatAI21 -``` - -## Embedding models - -### AI21 Embeddings - -See a [usage example](/docs/integrations/text_embedding/ai21). - -```python -from langchain_ai21 import AI21Embeddings -``` - -## Text splitters - -### AI21 Semantic Text Splitter - -See a [usage example](/docs/integrations/document_transformers/ai21_semantic_text_splitter). - -```python -from langchain_ai21 import AI21SemanticTextSplitter -``` - +``` \ No newline at end of file diff --git a/docs/docs/integrations/text_embedding/ai21.ipynb b/docs/docs/integrations/text_embedding/ai21.ipynb deleted file mode 100644 index c48d3e24a62b8..0000000000000 --- a/docs/docs/integrations/text_embedding/ai21.ipynb +++ /dev/null @@ -1,270 +0,0 @@ -{ - "cells": [ - { - "cell_type": "raw", - "id": "afaf8039", - "metadata": {}, - "source": [ - "---\n", - "sidebar_label: AI21\n", - "---" - ] - }, - { - "cell_type": "markdown", - "id": "9a3d6f34", - "metadata": {}, - "source": [ - "# AI21Embeddings\n", - "\n", - "This will help you get started with AI21 embedding models using LangChain. For detailed documentation on `AI21Embeddings` features and configuration options, please refer to the [API reference](https://python.langchain.com/api_reference/ai21/embeddings/langchain_ai21.embeddings.AI21Embeddings.html).\n", - "\n", - "## Overview\n", - "### Integration details\n", - "\n", - "import { ItemTable } from \"@theme/FeatureTables\";\n", - "\n", - "\n", - "\n", - "## Setup\n", - "\n", - "To access AI21 embedding models you'll need to create an AI21 account, get an API key, and install the `langchain-ai21` integration package.\n", - "\n", - "### Credentials\n", - "\n", - "Head to [https://docs.ai21.com/](https://docs.ai21.com/) to sign up to AI21 and generate an API key. Once you've done this set the `AI21_API_KEY` environment variable:" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "36521c2a", - "metadata": {}, - "outputs": [], - "source": [ - "import getpass\n", - "import os\n", - "\n", - "if not os.getenv(\"AI21_API_KEY\"):\n", - " os.environ[\"AI21_API_KEY\"] = getpass.getpass(\"Enter your AI21 API key: \")" - ] - }, - { - "cell_type": "markdown", - "id": "c84fb993", - "metadata": {}, - "source": [ - "If you want to get automated tracing of your model calls you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "39a4953b", - "metadata": {}, - "outputs": [], - "source": [ - "# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", - "# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")" - ] - }, - { - "cell_type": "markdown", - "id": "d9664366", - "metadata": {}, - "source": [ - "### Installation\n", - "\n", - "The LangChain AI21 integration lives in the `langchain-ai21` package:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "64853226", - "metadata": {}, - "outputs": [], - "source": [ - "%pip install -qU langchain-ai21" - ] - }, - { - "cell_type": "markdown", - "id": "45dd1724", - "metadata": {}, - "source": [ - "## Instantiation\n", - "\n", - "Now we can instantiate our model object and generate chat completions:" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "9ea7a09b", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain_ai21 import AI21Embeddings\n", - "\n", - "embeddings = AI21Embeddings(\n", - " # Can optionally increase or decrease the batch_size\n", - " # to improve latency.\n", - " # Use larger batch sizes with smaller documents, and\n", - " # smaller batch sizes with larger documents.\n", - " # batch_size=256,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "id": "77d271b6", - "metadata": {}, - "source": [ - "## Indexing and Retrieval\n", - "\n", - "Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our RAG tutorials under the [working with external knowledge tutorials](/docs/tutorials/#working-with-external-knowledge).\n", - "\n", - "Below, see how to index and retrieve data using the `embeddings` object we initialized above. In this example, we will index and retrieve a sample document in the `InMemoryVectorStore`." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "d817716b", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'LangChain is the framework for building context-aware reasoning applications'" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Create a vector store with a sample text\n", - "from langchain_core.vectorstores import InMemoryVectorStore\n", - "\n", - "text = \"LangChain is the framework for building context-aware reasoning applications\"\n", - "\n", - "vectorstore = InMemoryVectorStore.from_texts(\n", - " [text],\n", - " embedding=embeddings,\n", - ")\n", - "\n", - "# Use the vectorstore as a retriever\n", - "retriever = vectorstore.as_retriever()\n", - "\n", - "# Retrieve the most similar text\n", - "retrieved_documents = retriever.invoke(\"What is LangChain?\")\n", - "\n", - "# show the retrieved document's content\n", - "retrieved_documents[0].page_content" - ] - }, - { - "cell_type": "markdown", - "id": "e02b9855", - "metadata": {}, - "source": [ - "## Direct Usage\n", - "\n", - "Under the hood, the vectorstore and retriever implementations are calling `embeddings.embed_documents(...)` and `embeddings.embed_query(...)` to create embeddings for the text(s) used in `from_texts` and retrieval `invoke` operations, respectively.\n", - "\n", - "You can directly call these methods to get embeddings for your own use cases.\n", - "\n", - "### Embed single texts\n", - "\n", - "You can embed single texts or documents with `embed_query`:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "0d2befcd", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0.01913362182676792, 0.004960147198289633, -0.01582135073840618, -0.042474791407585144, 0.040200788\n" - ] - } - ], - "source": [ - "single_vector = embeddings.embed_query(text)\n", - "print(str(single_vector)[:100]) # Show the first 100 characters of the vector" - ] - }, - { - "cell_type": "markdown", - "id": "1b5a7d03", - "metadata": {}, - "source": [ - "### Embed multiple texts\n", - "\n", - "You can embed multiple texts with `embed_documents`:" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "2f4d6e97", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0.03029559925198555, 0.002908500377088785, -0.02700909972190857, -0.04616579785943031, 0.0382771529\n", - "[0.018214847892522812, 0.011460083536803722, -0.03329407051205635, -0.04951060563325882, 0.032756105\n" - ] - } - ], - "source": [ - "text2 = (\n", - " \"LangGraph is a library for building stateful, multi-actor applications with LLMs\"\n", - ")\n", - "two_vectors = embeddings.embed_documents([text, text2])\n", - "for vector in two_vectors:\n", - " print(str(vector)[:100]) # Show the first 100 characters of the vector" - ] - }, - { - "cell_type": "markdown", - "id": "98785c12", - "metadata": {}, - "source": [ - "## API Reference\n", - "\n", - "For detailed documentation on `AI21Embeddings` features and configuration options, please refer to the [API reference](https://python.langchain.com/api_reference/ai21/embeddings/langchain_ai21.embeddings.AI21Embeddings.html).\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}