mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-11 11:40:19 +00:00
Compare commits
1 Commits
erick/rele
...
eugene/com
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af919183a4 |
8
.github/scripts/check_diff.py
vendored
8
.github/scripts/check_diff.py
vendored
@@ -19,7 +19,6 @@ if __name__ == "__main__":
|
||||
"test": set(),
|
||||
"extended-test": set(),
|
||||
}
|
||||
docs_edited = False
|
||||
|
||||
if len(files) == 300:
|
||||
# max diff length is 300 files - there are likely files missing
|
||||
@@ -54,10 +53,6 @@ if __name__ == "__main__":
|
||||
dirs_to_run["lint"].add("libs/standard-tests")
|
||||
dirs_to_run["test"].add("libs/partners/mistralai")
|
||||
dirs_to_run["test"].add("libs/partners/openai")
|
||||
dirs_to_run["test"].add("libs/partners/anthropic")
|
||||
dirs_to_run["test"].add("libs/partners/ai21")
|
||||
dirs_to_run["test"].add("libs/partners/fireworks")
|
||||
dirs_to_run["test"].add("libs/partners/groq")
|
||||
|
||||
elif file.startswith("libs/cli"):
|
||||
# todo: add cli makefile
|
||||
@@ -77,8 +72,6 @@ if __name__ == "__main__":
|
||||
"an update for this new library!"
|
||||
)
|
||||
elif any(file.startswith(p) for p in ["docs/", "templates/", "cookbook/"]):
|
||||
if file.startswith("docs/"):
|
||||
docs_edited = True
|
||||
dirs_to_run["lint"].add(".")
|
||||
|
||||
outputs = {
|
||||
@@ -87,7 +80,6 @@ if __name__ == "__main__":
|
||||
),
|
||||
"dirs-to-test": list(dirs_to_run["test"] | dirs_to_run["extended-test"]),
|
||||
"dirs-to-extended-test": list(dirs_to_run["extended-test"]),
|
||||
"docs-edited": "true" if docs_edited else "",
|
||||
}
|
||||
for key, value in outputs.items():
|
||||
json_output = json.dumps(value)
|
||||
|
||||
2
.github/workflows/_integration_test.yml
vendored
2
.github/workflows/_integration_test.yml
vendored
@@ -58,7 +58,6 @@ jobs:
|
||||
MISTRAL_API_KEY: ${{ secrets.MISTRAL_API_KEY }}
|
||||
TOGETHER_API_KEY: ${{ secrets.TOGETHER_API_KEY }}
|
||||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||
GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
|
||||
NVIDIA_API_KEY: ${{ secrets.NVIDIA_API_KEY }}
|
||||
GOOGLE_SEARCH_API_KEY: ${{ secrets.GOOGLE_SEARCH_API_KEY }}
|
||||
GOOGLE_CSE_ID: ${{ secrets.GOOGLE_CSE_ID }}
|
||||
@@ -78,7 +77,6 @@ jobs:
|
||||
MONGODB_ATLAS_URI: ${{ secrets.MONGODB_ATLAS_URI }}
|
||||
VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }}
|
||||
COHERE_API_KEY: ${{ secrets.COHERE_API_KEY }}
|
||||
UPSTAGE_API_KEY: ${{ secrets.UPSTAGE_API_KEY }}
|
||||
run: |
|
||||
make integration_tests
|
||||
|
||||
|
||||
1
.github/workflows/_release.yml
vendored
1
.github/workflows/_release.yml
vendored
@@ -215,7 +215,6 @@ jobs:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # for airbyte
|
||||
MONGODB_ATLAS_URI: ${{ secrets.MONGODB_ATLAS_URI }}
|
||||
VOYAGE_API_KEY: ${{ secrets.VOYAGE_API_KEY }}
|
||||
UPSTAGE_API_KEY: ${{ secrets.UPSTAGE_API_KEY }}
|
||||
run: make integration_tests
|
||||
working-directory: ${{ inputs.working-directory }}
|
||||
|
||||
|
||||
59
.github/workflows/_release_note_experiments.yml
vendored
59
.github/workflows/_release_note_experiments.yml
vendored
@@ -1,59 +0,0 @@
|
||||
name: release note experiments
|
||||
run-name: Release note for ${{ inputs.working-directory }} by @${{ github.actor }}
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
working-directory:
|
||||
required: true
|
||||
type: string
|
||||
default: 'libs/langchain'
|
||||
|
||||
env:
|
||||
PYTHON_VERSION: "3.11"
|
||||
POETRY_VERSION: "1.7.1"
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
outputs:
|
||||
pkg-name: ${{ steps.check-version.outputs.pkg-name }}
|
||||
version: ${{ steps.check-version.outputs.version }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up Python + Poetry ${{ env.POETRY_VERSION }}
|
||||
uses: "./.github/actions/poetry_setup"
|
||||
with:
|
||||
python-version: ${{ env.PYTHON_VERSION }}
|
||||
poetry-version: ${{ env.POETRY_VERSION }}
|
||||
working-directory: ${{ inputs.working-directory }}
|
||||
cache-key: release
|
||||
- name: Check Version
|
||||
id: check-version
|
||||
shell: bash
|
||||
working-directory: ${{ inputs.working-directory }}
|
||||
run: |
|
||||
echo pkg-name="$(poetry version | cut -d ' ' -f 1)" >> $GITHUB_OUTPUT
|
||||
echo version="$(poetry version --short)" >> $GITHUB_OUTPUT
|
||||
release-notes:
|
||||
needs:
|
||||
- build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python + Poetry ${{ env.POETRY_VERSION }}
|
||||
uses: "./.github/actions/poetry_setup"
|
||||
with:
|
||||
python-version: ${{ env.PYTHON_VERSION }}
|
||||
poetry-version: ${{ env.POETRY_VERSION }}
|
||||
working-directory: ${{ inputs.working-directory }}
|
||||
cache-key: release
|
||||
- name: Generate Release Notes
|
||||
env:
|
||||
TAG_NAME: ${{ needs.build.outputs.pkg-name }}-v${{ needs.build.outputs.version }}
|
||||
RELEASE_NAME: ${{ needs.build.outputs.pkg-name }}==${{ needs.build.outputs.version }}
|
||||
run: |
|
||||
echo "TAG_NAME=${TAG_NAME}"
|
||||
echo "RELEASE_NAME=${RELEASE_NAME}"
|
||||
7
.github/workflows/check_diffs.yml
vendored
7
.github/workflows/check_diffs.yml
vendored
@@ -36,7 +36,6 @@ jobs:
|
||||
dirs-to-lint: ${{ steps.set-matrix.outputs.dirs-to-lint }}
|
||||
dirs-to-test: ${{ steps.set-matrix.outputs.dirs-to-test }}
|
||||
dirs-to-extended-test: ${{ steps.set-matrix.outputs.dirs-to-extended-test }}
|
||||
docs-edited: ${{ steps.set-matrix.outputs.docs-edited }}
|
||||
lint:
|
||||
name: cd ${{ matrix.working-directory }}
|
||||
needs: [ build ]
|
||||
@@ -61,9 +60,9 @@ jobs:
|
||||
working-directory: ${{ matrix.working-directory }}
|
||||
secrets: inherit
|
||||
|
||||
test-doc-imports:
|
||||
test_doc_imports:
|
||||
needs: [ build ]
|
||||
if: ${{ needs.build.outputs.dirs-to-test != '[]' || needs.build.outputs.docs-edited }}
|
||||
if: ${{ needs.build.outputs.dirs-to-test != '[]' }}
|
||||
uses: ./.github/workflows/_test_doc_imports.yml
|
||||
secrets: inherit
|
||||
|
||||
@@ -141,7 +140,7 @@ jobs:
|
||||
echo "$STATUS" | grep 'nothing to commit, working tree clean'
|
||||
ci_success:
|
||||
name: "CI Success"
|
||||
needs: [build, lint, test, compile-integration-tests, dependencies, extended-tests, test-doc-imports]
|
||||
needs: [build, lint, test, compile-integration-tests, dependencies, extended-tests]
|
||||
if: |
|
||||
always()
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
42
.github/workflows/scheduled_test.yml
vendored
42
.github/workflows/scheduled_test.yml
vendored
@@ -10,21 +10,19 @@ env:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
defaults:
|
||||
run:
|
||||
working-directory: libs/langchain
|
||||
runs-on: ubuntu-latest
|
||||
environment: Scheduled testing
|
||||
strategy:
|
||||
matrix:
|
||||
python-version:
|
||||
- "3.8"
|
||||
- "3.9"
|
||||
- "3.10"
|
||||
- "3.11"
|
||||
working-directory:
|
||||
- "libs/partners/openai"
|
||||
- "libs/partners/anthropic"
|
||||
# - "libs/partners/ai21" # standard-tests broken
|
||||
- "libs/partners/fireworks"
|
||||
# - "libs/partners/groq" # rate-limited
|
||||
- "libs/partners/mistralai"
|
||||
# - "libs/partners/together" # rate-limited
|
||||
name: Python ${{ matrix.python-version }} - ${{ matrix.working-directory }}
|
||||
name: Python ${{ matrix.python-version }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
@@ -33,7 +31,7 @@ jobs:
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
poetry-version: ${{ env.POETRY_VERSION }}
|
||||
working-directory: ${{ matrix.working-directory }}
|
||||
working-directory: libs/langchain
|
||||
cache-key: scheduled
|
||||
|
||||
- name: 'Authenticate to Google Cloud'
|
||||
@@ -42,15 +40,26 @@ jobs:
|
||||
with:
|
||||
credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}'
|
||||
|
||||
- name: Configure AWS Credentials
|
||||
uses: aws-actions/configure-aws-credentials@v4
|
||||
with:
|
||||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
|
||||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
||||
aws-region: ${{ vars.AWS_REGION }}
|
||||
|
||||
- name: Install dependencies
|
||||
working-directory: ${{ matrix.working-directory }}
|
||||
working-directory: libs/langchain
|
||||
shell: bash
|
||||
run: |
|
||||
echo "Running scheduled tests, installing dependencies with poetry..."
|
||||
poetry install --with=test_integration,test
|
||||
|
||||
- name: Run integration tests
|
||||
working-directory: ${{ matrix.working-directory }}
|
||||
- name: Install deps outside pyproject
|
||||
if: ${{ startsWith(inputs.working-directory, 'libs/community/') }}
|
||||
shell: bash
|
||||
run: poetry run pip install "boto3<2" "google-cloud-aiplatform<2"
|
||||
|
||||
- name: Run tests
|
||||
shell: bash
|
||||
env:
|
||||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||
@@ -61,16 +70,11 @@ jobs:
|
||||
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME: ${{ secrets.AZURE_OPENAI_CHAT_DEPLOYMENT_NAME }}
|
||||
AZURE_OPENAI_LLM_DEPLOYMENT_NAME: ${{ secrets.AZURE_OPENAI_LLM_DEPLOYMENT_NAME }}
|
||||
AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME: ${{ secrets.AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME }}
|
||||
AI21_API_KEY: ${{ secrets.AI21_API_KEY }}
|
||||
FIREWORKS_API_KEY: ${{ secrets.FIREWORKS_API_KEY }}
|
||||
GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
|
||||
MISTRAL_API_KEY: ${{ secrets.MISTRAL_API_KEY }}
|
||||
TOGETHER_API_KEY: ${{ secrets.TOGETHER_API_KEY }}
|
||||
run: |
|
||||
make integration_test
|
||||
make scheduled_tests
|
||||
|
||||
- name: Ensure the tests did not create any additional files
|
||||
working-directory: ${{ matrix.working-directory }}
|
||||
shell: bash
|
||||
run: |
|
||||
set -eu
|
||||
|
||||
@@ -47,7 +47,7 @@ For these applications, LangChain simplifies the entire application lifecycle:
|
||||
- **`langchain-community`**: Third party integrations.
|
||||
- Some integrations have been further split into **partner packages** that only rely on **`langchain-core`**. Examples include **`langchain_openai`** and **`langchain_anthropic`**.
|
||||
- **`langchain`**: Chains, agents, and retrieval strategies that make up an application's cognitive architecture.
|
||||
- **[LangGraph](https://python.langchain.com/docs/langgraph)**: A library for building robust and stateful multi-actor applications with LLMs by modeling steps as edges and nodes in a graph.
|
||||
- **`[LangGraph](https://python.langchain.com/docs/langgraph)`**: A library for building robust and stateful multi-actor applications with LLMs by modeling steps as edges and nodes in a graph.
|
||||
|
||||
### Productionization:
|
||||
- **[LangSmith](https://python.langchain.com/docs/langsmith)**: A developer platform that lets you debug, test, evaluate, and monitor chains built on any LLM framework and seamlessly integrates with LangChain.
|
||||
|
||||
@@ -535,9 +535,9 @@
|
||||
" print(f\"--Generated {len(all_clusters)} clusters--\")\n",
|
||||
"\n",
|
||||
" # Summarization\n",
|
||||
" template = \"\"\"Here is a sub-set of LangChain Expression Language doc. \n",
|
||||
" template = \"\"\"Here is a sub-set of LangChain Expression Langauge doc. \n",
|
||||
" \n",
|
||||
" LangChain Expression Language provides a way to compose chain in LangChain.\n",
|
||||
" LangChain Expression Langauge provides a way to compose chain in LangChain.\n",
|
||||
" \n",
|
||||
" Give a detailed summary of the documentation provided.\n",
|
||||
" \n",
|
||||
|
||||
@@ -9,10 +9,6 @@
|
||||
|
||||
## Tutorials
|
||||
|
||||
### [LangChain v 0.1 by LangChain.ai](https://www.youtube.com/playlist?list=PLfaIDFEXuae0gBSJ9T0w7cu7iJZbH3T31)
|
||||
### [Build with Langchain - Advanced by LangChain.ai](https://www.youtube.com/playlist?list=PLfaIDFEXuae06tclDATrMYY0idsTdLg9v)
|
||||
### [LangGraph by LangChain.ai](https://www.youtube.com/playlist?list=PLfaIDFEXuae16n2TWUkKq5PgJ0w6Pkwtg)
|
||||
|
||||
### [by Greg Kamradt](https://www.youtube.com/playlist?list=PLqZXAkvF1bPNQER9mLmDbntNfSpzdDIU5)
|
||||
### [by Sam Witteveen](https://www.youtube.com/playlist?list=PL8motc6AQftk1Bs42EW45kwYbyJ4jOdiZ)
|
||||
### [by James Briggs](https://www.youtube.com/playlist?list=PLIUOU7oqGTLieV9uTIFMm6_4PXg-hlN6F)
|
||||
@@ -39,7 +35,6 @@
|
||||
- [Udacity](https://www.udacity.com/catalog/all/any-price/any-school/any-skill/any-difficulty/any-duration/any-type/relevance/page-1?searchValue=langchain)
|
||||
- [LinkedIn Learning](https://www.linkedin.com/search/results/learning/?keywords=langchain)
|
||||
- [edX](https://www.edx.org/search?q=langchain)
|
||||
- [freeCodeCamp](https://www.youtube.com/@freecodecamp/search?query=langchain)
|
||||
|
||||
## Short Tutorials
|
||||
|
||||
|
||||
@@ -29,9 +29,9 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts import ChatPromptTemplate\n",
|
||||
"from langchain_community.vectorstores import FAISS\n",
|
||||
"from langchain_core.output_parsers import StrOutputParser\n",
|
||||
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||
"from langchain_core.runnables import RunnablePassthrough\n",
|
||||
"from langchain_openai import ChatOpenAI, OpenAIEmbeddings"
|
||||
]
|
||||
|
||||
@@ -63,7 +63,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_core.runnables import ConfigurableField\n",
|
||||
"from langchain_openai import ChatOpenAI\n",
|
||||
"\n",
|
||||
@@ -285,8 +285,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_community.chat_models import ChatAnthropic\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain_core.runnables import ConfigurableField\n",
|
||||
"from langchain_openai import ChatOpenAI"
|
||||
]
|
||||
|
||||
@@ -293,7 +293,7 @@ embeddings = OllamaEmbeddings()
|
||||
Make sure you have the `cohere` package installed and the appropriate environment variables set (these are the same as needed for the LLM).
|
||||
|
||||
```python
|
||||
from langchain_cohere.embeddings import CohereEmbeddings
|
||||
from langchain_community.embeddings import CohereEmbeddings
|
||||
|
||||
embeddings = CohereEmbeddings()
|
||||
```
|
||||
|
||||
@@ -137,7 +137,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_core.prompts.prompt import PromptTemplate\n",
|
||||
"from langchain.prompts.prompt import PromptTemplate\n",
|
||||
"from langchain_openai import ChatOpenAI\n",
|
||||
"\n",
|
||||
"anonymizer = PresidioAnonymizer()\n",
|
||||
|
||||
@@ -878,8 +878,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts.prompt import PromptTemplate\n",
|
||||
"from langchain_core.prompts import format_document\n",
|
||||
"from langchain_core.prompts.prompt import PromptTemplate\n",
|
||||
"\n",
|
||||
"DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template=\"{page_content}\")\n",
|
||||
"\n",
|
||||
|
||||
@@ -207,7 +207,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain_core.prompts.prompt import PromptTemplate\n",
|
||||
"from langchain.prompts.prompt import PromptTemplate\n",
|
||||
"from langchain_openai import ChatOpenAI\n",
|
||||
"\n",
|
||||
"anonymizer = PresidioReversibleAnonymizer()\n",
|
||||
|
||||
@@ -278,8 +278,8 @@
|
||||
],
|
||||
"source": [
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_core.callbacks.stdout import StdOutCallbackHandler\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain_openai import OpenAI\n",
|
||||
"\n",
|
||||
"argilla_callback = ArgillaCallbackHandler(\n",
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%pip install --upgrade --quiet langchain langchain-openai deepeval langchain-chroma"
|
||||
"%pip install --upgrade --quiet langchain langchain-openai deepeval"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -215,8 +215,8 @@
|
||||
"source": [
|
||||
"import requests\n",
|
||||
"from langchain.chains import RetrievalQA\n",
|
||||
"from langchain_chroma import Chroma\n",
|
||||
"from langchain_community.document_loaders import TextLoader\n",
|
||||
"from langchain_community.vectorstores import Chroma\n",
|
||||
"from langchain_openai import OpenAI, OpenAIEmbeddings\n",
|
||||
"from langchain_text_splitters import CharacterTextSplitter\n",
|
||||
"\n",
|
||||
|
||||
@@ -170,8 +170,8 @@
|
||||
"import os\n",
|
||||
"\n",
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain_core.prompts.chat import (\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain.prompts.chat import (\n",
|
||||
" ChatPromptTemplate,\n",
|
||||
" HumanMessagePromptTemplate,\n",
|
||||
")\n",
|
||||
|
||||
@@ -151,7 +151,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts import (\n",
|
||||
"from langchain.prompts import (\n",
|
||||
" ChatPromptTemplate,\n",
|
||||
" FewShotChatMessagePromptTemplate,\n",
|
||||
")\n",
|
||||
|
||||
@@ -91,7 +91,7 @@
|
||||
"source": [
|
||||
"from langchain.agents import initialize_agent, load_tools\n",
|
||||
"from langchain.chains import LLMChain, SimpleSequentialChain\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_openai import OpenAI\n",
|
||||
"from sagemaker.analytics import ExperimentAnalytics\n",
|
||||
"from sagemaker.experiments.run import Run\n",
|
||||
|
||||
@@ -1,503 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/langchain-ai/langchain/blob/master/docs/docs/integrations/callbacks/uptrain.ipynb\">\n",
|
||||
" <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
|
||||
"</a>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# UpTrain\n",
|
||||
"\n",
|
||||
"> UpTrain [[github](https://github.com/uptrain-ai/uptrain) || [website](https://uptrain.ai/) || [docs](https://docs.uptrain.ai/getting-started/introduction)] is an open-source platform to evaluate and improve LLM applications. It provides grades for 20+ preconfigured checks (covering language, code, embedding use cases), performs root cause analyses on instances of failure cases and provides guidance for resolving them."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## UpTrain Callback Handler\n",
|
||||
"\n",
|
||||
"This notebook showcases the UpTrain callback handler seamlessly integrating into your pipeline, facilitating diverse evaluations. We have chosen a few evaluations that we deemed apt for evaluating the chains. These evaluations run automatically, with results displayed in the output. More details on UpTrain's evaluations can be found [here](https://github.com/uptrain-ai/uptrain?tab=readme-ov-file#pre-built-evaluations-we-offer-). \n",
|
||||
"\n",
|
||||
"Selected retievers from Langchain are highlighted for demonstration:\n",
|
||||
"\n",
|
||||
"### 1. **Vanilla RAG**:\n",
|
||||
"RAG plays a crucial role in retrieving context and generating responses. To ensure its performance and response quality, we conduct the following evaluations:\n",
|
||||
"\n",
|
||||
"- **[Context Relevance](https://docs.uptrain.ai/predefined-evaluations/context-awareness/context-relevance)**: Determines if the context extracted from the query is relevant to the response.\n",
|
||||
"- **[Factual Accuracy](https://docs.uptrain.ai/predefined-evaluations/context-awareness/factual-accuracy)**: Assesses if the LLM is hallcuinating or providing incorrect information.\n",
|
||||
"- **[Response Completeness](https://docs.uptrain.ai/predefined-evaluations/response-quality/response-completeness)**: Checks if the response contains all the information requested by the query.\n",
|
||||
"\n",
|
||||
"### 2. **Multi Query Generation**:\n",
|
||||
"MultiQueryRetriever creates multiple variants of a question having a similar meaning to the original question. Given the complexity, we include the previous evaluations and add:\n",
|
||||
"\n",
|
||||
"- **[Multi Query Accuracy](https://docs.uptrain.ai/predefined-evaluations/query-quality/multi-query-accuracy)**: Assures that the multi-queries generated mean the same as the original query.\n",
|
||||
"\n",
|
||||
"### 3. **Context Compression and Reranking**:\n",
|
||||
"Re-ranking involves reordering nodes based on relevance to the query and choosing top n nodes. Since the number of nodes can reduce once the re-ranking is complete, we perform the following evaluations:\n",
|
||||
"\n",
|
||||
"- **[Context Reranking](https://docs.uptrain.ai/predefined-evaluations/context-awareness/context-reranking)**: Checks if the order of re-ranked nodes is more relevant to the query than the original order.\n",
|
||||
"- **[Context Conciseness](https://docs.uptrain.ai/predefined-evaluations/context-awareness/context-conciseness)**: Examines whether the reduced number of nodes still provides all the required information.\n",
|
||||
"\n",
|
||||
"These evaluations collectively ensure the robustness and effectiveness of the RAG, MultiQueryRetriever, and the Reranking process in the chain."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Install Dependencies"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
|
||||
"To disable this warning, you can either:\n",
|
||||
"\t- Avoid using `tokenizers` before the fork if possible\n",
|
||||
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[33mWARNING: There was an error checking the latest version of pip.\u001b[0m\u001b[33m\n",
|
||||
"\u001b[0mNote: you may need to restart the kernel to use updated packages.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%pip install -qU langchain langchain_openai uptrain faiss-cpu flashrank"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"NOTE: that you can also install `faiss-gpu` instead of `faiss-cpu` if you want to use the GPU enabled version of the library."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Import Libraries"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from getpass import getpass\n",
|
||||
"\n",
|
||||
"from langchain.chains import RetrievalQA\n",
|
||||
"from langchain.retrievers import ContextualCompressionRetriever\n",
|
||||
"from langchain.retrievers.document_compressors import FlashrankRerank\n",
|
||||
"from langchain.retrievers.multi_query import MultiQueryRetriever\n",
|
||||
"from langchain_community.callbacks.uptrain_callback import UpTrainCallbackHandler\n",
|
||||
"from langchain_community.document_loaders import TextLoader\n",
|
||||
"from langchain_community.vectorstores import FAISS\n",
|
||||
"from langchain_core.output_parsers.string import StrOutputParser\n",
|
||||
"from langchain_core.prompts.chat import ChatPromptTemplate\n",
|
||||
"from langchain_core.runnables.passthrough import RunnablePassthrough\n",
|
||||
"from langchain_openai import ChatOpenAI, OpenAIEmbeddings\n",
|
||||
"from langchain_text_splitters import (\n",
|
||||
" RecursiveCharacterTextSplitter,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Load the documents"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"loader = TextLoader(\"../../modules/state_of_the_union.txt\")\n",
|
||||
"documents = loader.load()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split the document into chunks"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
|
||||
"chunks = text_splitter.split_documents(documents)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Create the retriever"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"embeddings = OpenAIEmbeddings()\n",
|
||||
"db = FAISS.from_documents(chunks, embeddings)\n",
|
||||
"retriever = db.as_retriever()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Define the LLM"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = ChatOpenAI(temperature=0, model=\"gpt-4\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Set the openai API key\n",
|
||||
"This key is required to perform the evaluations. UpTrain uses the GPT models to evaluate the responses generated by the LLM."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"OPENAI_API_KEY = getpass()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
"For each of the retrievers below, it is better to define the callback handler again to avoid interference. You can choose between the following options for evaluating using UpTrain:\n",
|
||||
"\n",
|
||||
"### 1. **UpTrain's Open-Source Software (OSS)**: \n",
|
||||
"You can use the open-source evaluation service to evaluate your model.\n",
|
||||
"In this case, you will need to provie an OpenAI API key. You can get yours [here](https://platform.openai.com/account/api-keys).\n",
|
||||
"\n",
|
||||
"Parameters:\n",
|
||||
"- key_type=\"openai\"\n",
|
||||
"- api_key=\"OPENAI_API_KEY\"\n",
|
||||
"- project_name_prefix=\"PROJECT_NAME_PREFIX\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"### 2. **UpTrain Managed Service and Dashboards**: \n",
|
||||
"You can create a free UpTrain account [here](https://uptrain.ai/) and get free trial credits. If you want more trial credits, [book a call with the maintainers of UpTrain here](https://calendly.com/uptrain-sourabh/30min).\n",
|
||||
"\n",
|
||||
"UpTrain Managed service provides:\n",
|
||||
"1. Dashboards with advanced drill-down and filtering options\n",
|
||||
"1. Insights and common topics among failing cases\n",
|
||||
"1. Observability and real-time monitoring of production data\n",
|
||||
"1. Regression testing via seamless integration with your CI/CD pipelines\n",
|
||||
"\n",
|
||||
"The notebook contains some screenshots of the dashboards and the insights that you can get from the UpTrain managed service.\n",
|
||||
"\n",
|
||||
"Parameters:\n",
|
||||
"- key_type=\"uptrain\"\n",
|
||||
"- api_key=\"UPTRAIN_API_KEY\"\n",
|
||||
"- project_name_prefix=\"PROJECT_NAME_PREFIX\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"**Note:** The `project_name_prefix` will be used as prefix for the project names in the UpTrain dashboard. These will be different for different types of evals. For example, if you set project_name_prefix=\"langchain\" and perform the multi_query evaluation, the project name will be \"langchain_multi_query\"."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 1. Vanilla RAG"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"UpTrain callback handler will automatically capture the query, context and response once generated and will run the following three evaluations *(Graded from 0 to 1)* on the response:\n",
|
||||
"- **[Context Relevance](https://docs.uptrain.ai/predefined-evaluations/context-awareness/context-relevance)**: Check if the context extractedfrom the query is relevant to the response.\n",
|
||||
"- **[Factual Accuracy](https://docs.uptrain.ai/predefined-evaluations/context-awareness/factual-accuracy)**: Check how factually accurate the response is.\n",
|
||||
"- **[Response Completeness](https://docs.uptrain.ai/predefined-evaluations/response-quality/response-completeness)**: Check if the response contains all the information that the query is asking for."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[32m2024-04-17 17:03:44.969\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate_on_server\u001b[0m:\u001b[36m378\u001b[0m - \u001b[1mSending evaluation request for rows 0 to <50 to the Uptrain\u001b[0m\n",
|
||||
"\u001b[32m2024-04-17 17:04:05.809\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate\u001b[0m:\u001b[36m367\u001b[0m - \u001b[1mLocal server not running, start the server to log data and visualize in the dashboard!\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"Question: What did the president say about Ketanji Brown Jackson\n",
|
||||
"Response: The president mentioned that he had nominated Ketanji Brown Jackson to serve on the United States Supreme Court 4 days ago. He described her as one of the nation's top legal minds who will continue Justice Breyer’s legacy of excellence. He also mentioned that she is a former top litigator in private practice, a former federal public defender, and comes from a family of public school educators and police officers. He described her as a consensus builder and noted that since her nomination, she has received a broad range of support from various groups, including the Fraternal Order of Police and former judges appointed by both Democrats and Republicans.\n",
|
||||
"\n",
|
||||
"Context Relevance Score: 1.0\n",
|
||||
"Factual Accuracy Score: 1.0\n",
|
||||
"Response Completeness Score: 1.0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Create the RAG prompt\n",
|
||||
"template = \"\"\"Answer the question based only on the following context, which can include text and tables:\n",
|
||||
"{context}\n",
|
||||
"Question: {question}\n",
|
||||
"\"\"\"\n",
|
||||
"rag_prompt_text = ChatPromptTemplate.from_template(template)\n",
|
||||
"\n",
|
||||
"# Create the chain\n",
|
||||
"chain = (\n",
|
||||
" {\"context\": retriever, \"question\": RunnablePassthrough()}\n",
|
||||
" | rag_prompt_text\n",
|
||||
" | llm\n",
|
||||
" | StrOutputParser()\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Create the uptrain callback handler\n",
|
||||
"uptrain_callback = UpTrainCallbackHandler(key_type=\"openai\", api_key=OPENAI_API_KEY)\n",
|
||||
"config = {\"callbacks\": [uptrain_callback]}\n",
|
||||
"\n",
|
||||
"# Invoke the chain with a query\n",
|
||||
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
|
||||
"docs = chain.invoke(query, config=config)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 2. Multi Query Generation\n",
|
||||
"\n",
|
||||
"The **MultiQueryRetriever** is used to tackle the problem that the RAG pipeline might not return the best set of documents based on the query. It generates multiple queries that mean the same as the original query and then fetches documents for each.\n",
|
||||
"\n",
|
||||
"To evluate this retriever, UpTrain will run the following evaluation:\n",
|
||||
"- **[Multi Query Accuracy](https://docs.uptrain.ai/predefined-evaluations/query-quality/multi-query-accuracy)**: Checks if the multi-queries generated mean the same as the original query."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[32m2024-04-17 17:04:10.675\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate_on_server\u001b[0m:\u001b[36m378\u001b[0m - \u001b[1mSending evaluation request for rows 0 to <50 to the Uptrain\u001b[0m\n",
|
||||
"\u001b[32m2024-04-17 17:04:16.804\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate\u001b[0m:\u001b[36m367\u001b[0m - \u001b[1mLocal server not running, start the server to log data and visualize in the dashboard!\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"Question: What did the president say about Ketanji Brown Jackson\n",
|
||||
"Multi Queries:\n",
|
||||
" - How did the president comment on Ketanji Brown Jackson?\n",
|
||||
" - What were the president's remarks regarding Ketanji Brown Jackson?\n",
|
||||
" - What statements has the president made about Ketanji Brown Jackson?\n",
|
||||
"\n",
|
||||
"Multi Query Accuracy Score: 0.5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[32m2024-04-17 17:04:22.027\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate_on_server\u001b[0m:\u001b[36m378\u001b[0m - \u001b[1mSending evaluation request for rows 0 to <50 to the Uptrain\u001b[0m\n",
|
||||
"\u001b[32m2024-04-17 17:04:44.033\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate\u001b[0m:\u001b[36m367\u001b[0m - \u001b[1mLocal server not running, start the server to log data and visualize in the dashboard!\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"Question: What did the president say about Ketanji Brown Jackson\n",
|
||||
"Response: The president mentioned that he had nominated Circuit Court of Appeals Judge Ketanji Brown Jackson to serve on the United States Supreme Court 4 days ago. He described her as one of the nation's top legal minds who will continue Justice Breyer’s legacy of excellence. He also mentioned that since her nomination, she has received a broad range of support—from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.\n",
|
||||
"\n",
|
||||
"Context Relevance Score: 1.0\n",
|
||||
"Factual Accuracy Score: 1.0\n",
|
||||
"Response Completeness Score: 1.0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Create the retriever\n",
|
||||
"multi_query_retriever = MultiQueryRetriever.from_llm(retriever=retriever, llm=llm)\n",
|
||||
"\n",
|
||||
"# Create the uptrain callback\n",
|
||||
"uptrain_callback = UpTrainCallbackHandler(key_type=\"openai\", api_key=OPENAI_API_KEY)\n",
|
||||
"config = {\"callbacks\": [uptrain_callback]}\n",
|
||||
"\n",
|
||||
"# Create the RAG prompt\n",
|
||||
"template = \"\"\"Answer the question based only on the following context, which can include text and tables:\n",
|
||||
"{context}\n",
|
||||
"Question: {question}\n",
|
||||
"\"\"\"\n",
|
||||
"rag_prompt_text = ChatPromptTemplate.from_template(template)\n",
|
||||
"\n",
|
||||
"chain = (\n",
|
||||
" {\"context\": multi_query_retriever, \"question\": RunnablePassthrough()}\n",
|
||||
" | rag_prompt_text\n",
|
||||
" | llm\n",
|
||||
" | StrOutputParser()\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Invoke the chain with a query\n",
|
||||
"question = \"What did the president say about Ketanji Brown Jackson\"\n",
|
||||
"docs = chain.invoke(question, config=config)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 3. Context Compression and Reranking\n",
|
||||
"\n",
|
||||
"The reranking process involves reordering nodes based on relevance to the query and choosing the top n nodes. Since the number of nodes can reduce once the reranking is complete, we perform the following evaluations:\n",
|
||||
"- **[Context Reranking](https://docs.uptrain.ai/predefined-evaluations/context-awareness/context-reranking)**: Check if the order of re-ranked nodes is more relevant to the query than the original order.\n",
|
||||
"- **[Context Conciseness](https://docs.uptrain.ai/predefined-evaluations/context-awareness/context-conciseness)**: Check if the reduced number of nodes still provides all the required information."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[32m2024-04-17 17:04:46.462\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate_on_server\u001b[0m:\u001b[36m378\u001b[0m - \u001b[1mSending evaluation request for rows 0 to <50 to the Uptrain\u001b[0m\n",
|
||||
"\u001b[32m2024-04-17 17:04:53.561\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate\u001b[0m:\u001b[36m367\u001b[0m - \u001b[1mLocal server not running, start the server to log data and visualize in the dashboard!\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"Question: What did the president say about Ketanji Brown Jackson\n",
|
||||
"\n",
|
||||
"Context Conciseness Score: 0.0\n",
|
||||
"Context Reranking Score: 1.0\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[32m2024-04-17 17:04:56.947\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate_on_server\u001b[0m:\u001b[36m378\u001b[0m - \u001b[1mSending evaluation request for rows 0 to <50 to the Uptrain\u001b[0m\n",
|
||||
"\u001b[32m2024-04-17 17:05:16.551\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36muptrain.framework.evalllm\u001b[0m:\u001b[36mevaluate\u001b[0m:\u001b[36m367\u001b[0m - \u001b[1mLocal server not running, start the server to log data and visualize in the dashboard!\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"Question: What did the president say about Ketanji Brown Jackson\n",
|
||||
"Response: The President mentioned that he nominated Circuit Court of Appeals Judge Ketanji Brown Jackson to serve on the United States Supreme Court 4 days ago. He described her as one of the nation's top legal minds who will continue Justice Breyer’s legacy of excellence.\n",
|
||||
"\n",
|
||||
"Context Relevance Score: 1.0\n",
|
||||
"Factual Accuracy Score: 1.0\n",
|
||||
"Response Completeness Score: 0.5\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Create the retriever\n",
|
||||
"compressor = FlashrankRerank()\n",
|
||||
"compression_retriever = ContextualCompressionRetriever(\n",
|
||||
" base_compressor=compressor, base_retriever=retriever\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Create the chain\n",
|
||||
"chain = RetrievalQA.from_chain_type(llm=llm, retriever=compression_retriever)\n",
|
||||
"\n",
|
||||
"# Create the uptrain callback\n",
|
||||
"uptrain_callback = UpTrainCallbackHandler(key_type=\"openai\", api_key=OPENAI_API_KEY)\n",
|
||||
"config = {\"callbacks\": [uptrain_callback]}\n",
|
||||
"\n",
|
||||
"# Invoke the chain with a query\n",
|
||||
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
|
||||
"result = chain.invoke(query, config=config)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": ".venv",
|
||||
"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.7"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -19,85 +19,59 @@
|
||||
"\n",
|
||||
">[Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/overview) provides REST API access to OpenAI's powerful language models including the GPT-4, GPT-3.5-Turbo, and Embeddings model series. These models can be easily adapted to your specific task including but not limited to content generation, summarization, semantic search, and natural language to code translation. Users can access the service through REST APIs, Python SDK, or a web-based interface in the Azure OpenAI Studio.\n",
|
||||
"\n",
|
||||
"This notebook goes over how to connect to an Azure-hosted OpenAI endpoint. First, we need to install the `langchain-openai` package."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "d83ba7de",
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "raw"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"%pip install -qU langchain-openai"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e39133c8",
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "raw"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"Next, let's set some environment variables to help us connect to the Azure OpenAI service. You can find these values in the Azure portal."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1d8d73bd",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"os.environ[\"AZURE_OPENAI_API_KEY\"] = \"...\"\n",
|
||||
"os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"https://<your-endpoint>.openai.azure.com/\"\n",
|
||||
"os.environ[\"AZURE_OPENAI_API_VERSION\"] = \"2023-06-01-preview\"\n",
|
||||
"os.environ[\"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"] = \"chat\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e7b160f8",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Next, let's construct our model and chat with it:"
|
||||
"This notebook goes over how to connect to an Azure-hosted OpenAI endpoint. We recommend having version `openai>=1` installed."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "cbe4bb58-ba13-4355-8af9-cd990dc47a64",
|
||||
"id": "96164b42",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.messages import HumanMessage\n",
|
||||
"from langchain_openai import AzureChatOpenAI\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"model = AzureChatOpenAI(\n",
|
||||
" openai_api_version=os.environ[\"AZURE_OPENAI_API_VERSION\"],\n",
|
||||
" azure_deployment=os.environ[\"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"],\n",
|
||||
")"
|
||||
"from langchain_core.messages import HumanMessage\n",
|
||||
"from langchain_openai import AzureChatOpenAI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "cbe4bb58-ba13-4355-8af9-cd990dc47a64",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"os.environ[\"AZURE_OPENAI_API_KEY\"] = \"...\"\n",
|
||||
"os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"https://<your-endpoint>.openai.azure.com/\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "8161278f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = AzureChatOpenAI(\n",
|
||||
" openai_api_version=\"2023-05-15\",\n",
|
||||
" azure_deployment=\"your-deployment-name\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "99509140",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"AIMessage(content=\"J'adore programmer.\", response_metadata={'token_usage': {'completion_tokens': 6, 'prompt_tokens': 19, 'total_tokens': 25}, 'model_name': 'gpt-35-turbo', 'system_fingerprint': None, 'prompt_filter_results': [{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}], 'finish_reason': 'stop', 'logprobs': None, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}, id='run-25ed88db-38f2-4b0c-a943-a03f217711a9-0')"
|
||||
"AIMessage(content=\"J'adore la programmation.\")"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -106,7 +80,7 @@
|
||||
"message = HumanMessage(\n",
|
||||
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
||||
")\n",
|
||||
"model.invoke([message])"
|
||||
"model([message])"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -122,7 +96,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 8,
|
||||
"id": "0531798a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -132,29 +106,19 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": null,
|
||||
"id": "aceddb72",
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Total Cost (USD): $0.000041\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = AzureChatOpenAI(\n",
|
||||
" openai_api_version=os.environ[\"AZURE_OPENAI_API_VERSION\"],\n",
|
||||
" azure_deployment=os.environ[\n",
|
||||
" \"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"\n",
|
||||
" ], # in Azure, this deployment has version 0613 - input and output tokens are counted separately\n",
|
||||
" openai_api_version=\"2023-05-15\",\n",
|
||||
" azure_deployment=\"gpt-35-turbo\", # in Azure, this deployment has version 0613 - input and output tokens are counted separately\n",
|
||||
")\n",
|
||||
"with get_openai_callback() as cb:\n",
|
||||
" model.invoke([message])\n",
|
||||
" model([message])\n",
|
||||
" print(\n",
|
||||
" f\"Total Cost (USD): ${format(cb.total_cost, '.6f')}\"\n",
|
||||
" ) # without specifying the model version, flat-rate 0.002 USD per 1k input and output tokens is used"
|
||||
@@ -170,7 +134,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 17,
|
||||
"id": "8d5e54e9",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -183,13 +147,13 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model0301 = AzureChatOpenAI(\n",
|
||||
" openai_api_version=os.environ[\"AZURE_OPENAI_API_VERSION\"],\n",
|
||||
" azure_deployment=os.environ[\"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME\"],\n",
|
||||
" model_version=\"0301\",\n",
|
||||
"model0613 = AzureChatOpenAI(\n",
|
||||
" openai_api_version=\"2023-05-15\",\n",
|
||||
" deployment_name=\"gpt-35-turbo\",\n",
|
||||
" model_version=\"0613\",\n",
|
||||
")\n",
|
||||
"with get_openai_callback() as cb:\n",
|
||||
" model0301.invoke([message])\n",
|
||||
" model0613([message])\n",
|
||||
" print(f\"Total Cost (USD): ${format(cb.total_cost, '.6f')}\")"
|
||||
]
|
||||
}
|
||||
@@ -210,7 +174,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.4"
|
||||
"version": "3.10.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -3,14 +3,10 @@
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "fbc66410",
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "raw"
|
||||
}
|
||||
},
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---\n",
|
||||
"sidebar_label: Bedrock\n",
|
||||
"sidebar_label: Bedrock Chat\n",
|
||||
"---"
|
||||
]
|
||||
},
|
||||
@@ -19,7 +15,7 @@
|
||||
"id": "bf733a38-db84-4363-89e2-de6735c37230",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# ChatBedrock\n",
|
||||
"# BedrockChat\n",
|
||||
"\n",
|
||||
">[Amazon Bedrock](https://aws.amazon.com/bedrock/) is a fully managed service that offers a choice of \n",
|
||||
"> high-performing foundation models (FMs) from leading AI companies like `AI21 Labs`, `Anthropic`, `Cohere`, \n",
|
||||
@@ -34,53 +30,42 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": null,
|
||||
"id": "d51edc81",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Note: you may need to restart the kernel to use updated packages.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%pip install --upgrade --quiet langchain-aws"
|
||||
"%pip install --upgrade --quiet boto3"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_aws import ChatBedrock\n",
|
||||
"from langchain_community.chat_models import BedrockChat\n",
|
||||
"from langchain_core.messages import HumanMessage"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 2,
|
||||
"id": "70cf04e8-423a-4ff6-8b09-f11fb711c817",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"chat = ChatBedrock(\n",
|
||||
" model_id=\"anthropic.claude-3-sonnet-20240229-v1:0\",\n",
|
||||
" model_kwargs={\"temperature\": 0.1},\n",
|
||||
")"
|
||||
"chat = BedrockChat(model_id=\"anthropic.claude-v2\", model_kwargs={\"temperature\": 0.1})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 3,
|
||||
"id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
@@ -89,10 +74,10 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"AIMessage(content=\"Voici la traduction en français :\\n\\nJ'aime la programmation.\", additional_kwargs={'usage': {'prompt_tokens': 20, 'completion_tokens': 21, 'total_tokens': 41}}, response_metadata={'model_id': 'anthropic.claude-3-sonnet-20240229-v1:0', 'usage': {'prompt_tokens': 20, 'completion_tokens': 21, 'total_tokens': 41}}, id='run-994f0362-0e50-4524-afad-3c4f5bb11328-0')"
|
||||
"AIMessage(content=\" Voici la traduction en français : J'adore programmer.\", additional_kwargs={}, example=False)"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -103,7 +88,7 @@
|
||||
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
||||
" )\n",
|
||||
"]\n",
|
||||
"chat.invoke(messages)"
|
||||
"chat(messages)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -112,30 +97,39 @@
|
||||
"id": "a4a4f4d4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Streaming\n",
|
||||
"\n",
|
||||
"To stream responses, you can use the runnable `.stream()` method."
|
||||
"### For BedrockChat with Streaming"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": null,
|
||||
"id": "c253883f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n",
|
||||
"\n",
|
||||
"chat = BedrockChat(\n",
|
||||
" model_id=\"anthropic.claude-v2\",\n",
|
||||
" streaming=True,\n",
|
||||
" callbacks=[StreamingStdOutCallbackHandler()],\n",
|
||||
" model_kwargs={\"temperature\": 0.1},\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d9e52838",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Voici la traduction en français :\n",
|
||||
"\n",
|
||||
"J'aime la programmation."
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"for chunk in chat.stream(messages):\n",
|
||||
" print(chunk.content, end=\"\", flush=True)"
|
||||
"messages = [\n",
|
||||
" HumanMessage(\n",
|
||||
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
||||
" )\n",
|
||||
"]\n",
|
||||
"chat(messages)"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -155,7 +149,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.4"
|
||||
"version": "3.10.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -19,13 +19,13 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.chat_models import JinaChat\n",
|
||||
"from langchain_core.messages import HumanMessage, SystemMessage\n",
|
||||
"from langchain_core.prompts.chat import (\n",
|
||||
"from langchain.prompts.chat import (\n",
|
||||
" ChatPromptTemplate,\n",
|
||||
" HumanMessagePromptTemplate,\n",
|
||||
" SystemMessagePromptTemplate,\n",
|
||||
")"
|
||||
")\n",
|
||||
"from langchain_community.chat_models import JinaChat\n",
|
||||
"from langchain_core.messages import HumanMessage, SystemMessage"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -49,12 +49,12 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.messages import SystemMessage\n",
|
||||
"from langchain_core.prompts.chat import (\n",
|
||||
"from langchain.prompts.chat import (\n",
|
||||
" ChatPromptTemplate,\n",
|
||||
" HumanMessagePromptTemplate,\n",
|
||||
" MessagesPlaceholder,\n",
|
||||
")\n",
|
||||
"from langchain_core.messages import SystemMessage\n",
|
||||
"\n",
|
||||
"template_messages = [\n",
|
||||
" SystemMessage(content=\"You are a helpful assistant.\"),\n",
|
||||
|
||||
@@ -60,9 +60,9 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts.chat import ChatPromptTemplate\n",
|
||||
"from langchain_community.chat_models import ChatMaritalk\n",
|
||||
"from langchain_core.output_parsers import StrOutputParser\n",
|
||||
"from langchain_core.prompts.chat import ChatPromptTemplate\n",
|
||||
"\n",
|
||||
"llm = ChatMaritalk(\n",
|
||||
" model=\"sabia-2-medium\", # Available models: sabia-2-small and sabia-2-medium\n",
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# ChatOctoAI\n",
|
||||
"\n",
|
||||
"[OctoAI](https://docs.octoai.cloud/docs) offers easy access to efficient compute and enables users to integrate their choice of AI models into applications. The `OctoAI` compute service helps you run, tune, and scale AI applications easily.\n",
|
||||
"\n",
|
||||
"This notebook demonstrates the use of `langchain.chat_models.ChatOctoAI` for [OctoAI endpoints](https://octoai.cloud/text).\n",
|
||||
"\n",
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
"To run our example app, there are two simple steps to take:\n",
|
||||
"\n",
|
||||
"1. Get an API Token from [your OctoAI account page](https://octoai.cloud/settings).\n",
|
||||
" \n",
|
||||
"2. Paste your API token in in the code cell below or use the `octoai_api_token` keyword argument.\n",
|
||||
"\n",
|
||||
"Note: If you want to use a different model than the [available models](https://octoai.cloud/text?selectedTags=Chat), you can containerize the model and make a custom OctoAI endpoint yourself, by following [Build a Container from Python](https://octo.ai/docs/bring-your-own-model/advanced-build-a-container-from-scratch-in-python) and [Create a Custom Endpoint from a Container](https://octo.ai/docs/bring-your-own-model/create-custom-endpoints-from-a-container/create-custom-endpoints-from-a-container) and then updating your `OCTOAI_API_BASE` environment variable.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"os.environ[\"OCTOAI_API_TOKEN\"] = \"OCTOAI_API_TOKEN\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.chat_models import ChatOctoAI\n",
|
||||
"from langchain_core.messages import HumanMessage, SystemMessage"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Example"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"chat = ChatOctoAI(max_tokens=300, model_name=\"mixtral-8x7b-instruct\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"messages = [\n",
|
||||
" SystemMessage(content=\"You are a helpful assistant.\"),\n",
|
||||
" HumanMessage(content=\"Tell me about Leonardo da Vinci briefly.\"),\n",
|
||||
"]\n",
|
||||
"print(chat(messages).content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Leonardo da Vinci (1452-1519) was an Italian polymath who is often considered one of the greatest painters in history. However, his genius extended far beyond art. He was also a scientist, inventor, mathematician, engineer, anatomist, geologist, and cartographer.\n",
|
||||
"\n",
|
||||
"Da Vinci is best known for his paintings such as the Mona Lisa, The Last Supper, and The Virgin of the Rocks. His scientific studies were ahead of his time, and his notebooks contain detailed drawings and descriptions of various machines, human anatomy, and natural phenomena.\n",
|
||||
"\n",
|
||||
"Despite never receiving a formal education, da Vinci's insatiable curiosity and observational skills made him a pioneer in many fields. His work continues to inspire and influence artists, scientists, and thinkers today."
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.11.7"
|
||||
},
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "97697b63fdcee0a640856f91cb41326ad601964008c341809e43189d1cab1047"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
@@ -30,7 +30,7 @@
|
||||
"* [Download](https://ollama.ai/download) and install Ollama onto the available supported platforms (including Windows Subsystem for Linux)\n",
|
||||
"* Fetch available LLM model via `ollama pull <name-of-model>`\n",
|
||||
" * View a list of available models via the [model library](https://ollama.ai/library)\n",
|
||||
" * e.g., `ollama pull llama3`\n",
|
||||
" * e.g., for `Llama-7b`: `ollama pull llama2`\n",
|
||||
"* This will download the default tagged version of the model. Typically, the default points to the latest, smallest sized-parameter model.\n",
|
||||
"\n",
|
||||
"> On Mac, the models will be download to `~/.ollama/models`\n",
|
||||
@@ -46,7 +46,7 @@
|
||||
"\n",
|
||||
"You can see a full list of supported parameters on the [API reference page](https://api.python.langchain.com/en/latest/llms/langchain.llms.ollama.Ollama.html).\n",
|
||||
"\n",
|
||||
"If you are using a LLaMA `chat` model (e.g., `ollama pull llama3`) then you can use the `ChatOllama` interface.\n",
|
||||
"If you are using a LLaMA `chat` model (e.g., `ollama pull llama2:7b-chat`) then you can use the `ChatOllama` interface.\n",
|
||||
"\n",
|
||||
"This includes [special tokens](https://huggingface.co/blog/llama2#how-to-prompt-llama-2) for system message and user input.\n",
|
||||
"\n",
|
||||
@@ -65,7 +65,7 @@
|
||||
"\n",
|
||||
"```bash\n",
|
||||
"curl http://localhost:11434/api/generate -d '{\n",
|
||||
" \"model\": \"llama3\",\n",
|
||||
" \"model\": \"llama2\",\n",
|
||||
" \"prompt\":\"Why is the sky blue?\"\n",
|
||||
"}'\n",
|
||||
"```\n",
|
||||
@@ -86,9 +86,11 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Why did the astronaut break up with his girlfriend?\n",
|
||||
" Sure, here's a fun space-themed joke for you:\n",
|
||||
"\n",
|
||||
"Because he needed space!\n"
|
||||
"Why don't astronauts like broccoli? \n",
|
||||
"Because it has too many \"crisps\" in it!\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -100,7 +102,7 @@
|
||||
"\n",
|
||||
"# supports many more optional parameters. Hover on your `ChatOllama(...)`\n",
|
||||
"# class to view the latest available supported parameters\n",
|
||||
"llm = ChatOllama(model=\"llama3\")\n",
|
||||
"llm = ChatOllama(model=\"llama2\")\n",
|
||||
"prompt = ChatPromptTemplate.from_template(\"Tell me a short joke about {topic}\")\n",
|
||||
"\n",
|
||||
"# using LangChain Expressive Language chain syntax\n",
|
||||
@@ -123,14 +125,21 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Why\n",
|
||||
" Sure\n",
|
||||
",\n",
|
||||
" here\n",
|
||||
"'s\n",
|
||||
" a\n",
|
||||
" joke\n",
|
||||
":\n",
|
||||
" Why\n",
|
||||
" did\n",
|
||||
" the\n",
|
||||
" astronaut\n",
|
||||
@@ -139,18 +148,17 @@
|
||||
" with\n",
|
||||
" his\n",
|
||||
" girlfriend\n",
|
||||
" before\n",
|
||||
" going\n",
|
||||
" to\n",
|
||||
" Mars\n",
|
||||
"?\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Because\n",
|
||||
" Because\n",
|
||||
" he\n",
|
||||
" needed\n",
|
||||
" more\n",
|
||||
" space\n",
|
||||
"!\n",
|
||||
" to\n",
|
||||
" explore\n",
|
||||
".\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
@@ -171,9 +179,51 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" Sure\n",
|
||||
",\n",
|
||||
" here\n",
|
||||
"'s\n",
|
||||
" a\n",
|
||||
" little\n",
|
||||
" one\n",
|
||||
":\n",
|
||||
" Why\n",
|
||||
" did\n",
|
||||
" the\n",
|
||||
" rocket\n",
|
||||
" scientist\n",
|
||||
" break\n",
|
||||
" up\n",
|
||||
" with\n",
|
||||
" her\n",
|
||||
" partner\n",
|
||||
"?\n",
|
||||
" Because\n",
|
||||
" he\n",
|
||||
" couldn\n",
|
||||
"'t\n",
|
||||
" handle\n",
|
||||
" all\n",
|
||||
" her\n",
|
||||
" \"\n",
|
||||
"space\n",
|
||||
"y\n",
|
||||
"\"\n",
|
||||
" jokes\n",
|
||||
".\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"topic = {\"topic\": \"Space travel\"}\n",
|
||||
"\n",
|
||||
@@ -205,13 +255,13 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.chat_models import ChatOllama\n",
|
||||
"\n",
|
||||
"llm = ChatOllama(model=\"llama3\", format=\"json\", temperature=0)"
|
||||
"llm = ChatOllama(model=\"llama2\", format=\"json\", temperature=0)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -223,7 +273,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"content='{ \"morning\": \"blue\", \"noon\": \"clear blue\", \"afternoon\": \"hazy yellow\", \"evening\": \"orange-red\" }\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n \\n\\n\\n\\n\\n\\n ' id='run-e893700f-e2d0-4df8-ad86-17525dcee318-0'\n"
|
||||
"content='{\\n\"morning\": {\\n\"color\": \"light blue\"\\n},\\n\"noon\": {\\n\"color\": \"blue\"\\n},\\n\"afternoon\": {\\n\"color\": \"grayish-blue\"\\n},\\n\"evening\": {\\n\"color\": \"pinkish-orange\"\\n}\\n}'\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -242,7 +292,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 53,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -250,9 +300,13 @@
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"Name: John\n",
|
||||
"Age: 35\n",
|
||||
"Likes: Pizza\n"
|
||||
"{\n",
|
||||
"\"name\": \"John\",\n",
|
||||
"\"age\": 35,\n",
|
||||
"\"interests\": [\n",
|
||||
"\"pizza\"\n",
|
||||
"]\n",
|
||||
"}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -462,7 +516,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.8"
|
||||
"version": "3.9.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
80
docs/docs/integrations/chat/solar.ipynb
Normal file
80
docs/docs/integrations/chat/solar.ipynb
Normal file
@@ -0,0 +1,80 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "a9667088-04e1-4f67-8221-a0072a2d635f",
|
||||
"metadata": {
|
||||
"execution": {
|
||||
"iopub.execute_input": "2024-03-06T17:04:59.273702Z",
|
||||
"iopub.status.busy": "2024-03-06T17:04:59.272602Z",
|
||||
"iopub.status.idle": "2024-03-06T17:05:00.129177Z",
|
||||
"shell.execute_reply": "2024-03-06T17:05:00.124594Z",
|
||||
"shell.execute_reply.started": "2024-03-06T17:04:59.273646Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"AIMessage(content='저는 대형 언어 모델 프로젝트를 구축하고 싶습니다.')"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"os.environ[\"SOLAR_API_KEY\"] = \"SOLAR_API_KEY\"\n",
|
||||
"\n",
|
||||
"from langchain_community.chat_models.solar import SolarChat\n",
|
||||
"from langchain_core.messages import HumanMessage, SystemMessage\n",
|
||||
"\n",
|
||||
"chat = SolarChat(max_tokens=1024)\n",
|
||||
"\n",
|
||||
"messages = [\n",
|
||||
" SystemMessage(\n",
|
||||
" content=\"You are a helpful assistant who translates English to Korean.\"\n",
|
||||
" ),\n",
|
||||
" HumanMessage(\n",
|
||||
" content=\"Translate this sentence from English to Korean. I want to build a project of large language model.\"\n",
|
||||
" ),\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"chat.invoke(messages)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8cb792fe-2844-4969-a9e9-f4c0f97b1699",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"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.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "910f5772b6af13c9",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"---\n",
|
||||
"sidebar_label: Upstage\n",
|
||||
"---"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "433f5422ad8e1efa",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"# ChatUpstage\n",
|
||||
"\n",
|
||||
"This notebook covers how to get started with Upstage chat models.\n",
|
||||
"\n",
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install `langchain-upstage` package.\n",
|
||||
"\n",
|
||||
"```bash\n",
|
||||
"pip install -U langchain-upstage\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b3c5c4627fe95eae",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"source": [
|
||||
"## Environment Setup\n",
|
||||
"\n",
|
||||
"Make sure to set the following environment variables:\n",
|
||||
"\n",
|
||||
"- `UPSTAGE_API_KEY`: Your Upstage API key from [Upstage console](https://console.upstage.ai/).\n",
|
||||
"\n",
|
||||
"## Usage"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "20a0067b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"os.environ[\"UPSTAGE_API_KEY\"] = \"YOUR_API_KEY\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8a4d650d76a33494",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"is_executing": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||
"from langchain_upstage import ChatUpstage\n",
|
||||
"\n",
|
||||
"chat = ChatUpstage()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a1679b5cafaf88b9",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# using chat invoke\n",
|
||||
"chat.invoke(\"Hello, how are you?\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "698a788a63b5c3e5",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# using chat stream\n",
|
||||
"for m in chat.stream(\"Hello, how are you?\"):\n",
|
||||
" print(m)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "36f8a703",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Chaining"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "efa06617e5d4f6b2",
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# using chain\n",
|
||||
"prompt = ChatPromptTemplate.from_messages(\n",
|
||||
" [\n",
|
||||
" (\"system\", \"You are a helpful assistant that translates English to French.\"),\n",
|
||||
" (\"human\", \"Translate this sentence from English to French. {english_text}.\"),\n",
|
||||
" ]\n",
|
||||
")\n",
|
||||
"chain = prompt | chat\n",
|
||||
"\n",
|
||||
"chain.invoke({\"english_text\": \"Hello, how are you?\"})"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "3.9.13"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -31,12 +31,12 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.messages import HumanMessage, SystemMessage\n",
|
||||
"from langchain_core.prompts.chat import (\n",
|
||||
"from langchain.prompts.chat import (\n",
|
||||
" ChatPromptTemplate,\n",
|
||||
" HumanMessagePromptTemplate,\n",
|
||||
" SystemMessagePromptTemplate,\n",
|
||||
")\n",
|
||||
"from langchain_core.messages import HumanMessage, SystemMessage\n",
|
||||
"from langchain_openai import ChatOpenAI"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -348,7 +348,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"async def ainvoke_with_prompt_template():\n",
|
||||
" from langchain_core.prompts.chat import (\n",
|
||||
" from langchain.prompts.chat import (\n",
|
||||
" ChatPromptTemplate,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
|
||||
@@ -17,7 +17,9 @@
|
||||
"\n",
|
||||
"This notebook shows how to use [ZHIPU AI API](https://open.bigmodel.cn/dev/api) in LangChain with the langchain.chat_models.ChatZhipuAI.\n",
|
||||
"\n",
|
||||
">[*GLM-4*](https://open.bigmodel.cn/) is a multi-lingual large language model aligned with human intent, featuring capabilities in Q&A, multi-turn dialogue, and code generation. The overall performance of the new generation base model GLM-4 has been significantly improved compared to the previous generation, supporting longer contexts; Stronger multimodality; Support faster inference speed, more concurrency, greatly reducing inference costs; Meanwhile, GLM-4 enhances the capabilities of intelligent agents.\n",
|
||||
">[*ZHIPU AI*](https://open.bigmodel.cn/) is a multi-lingual large language model aligned with human intent, featuring capabilities in Q&A, multi-turn dialogue, and code generation, developed on the foundation of the ChatGLM3. \n",
|
||||
"\n",
|
||||
">It's co-developed with Tsinghua University's KEG Laboratory under the ChatGLM3 project, signifying a new era in dialogue pre-training models. The open-source [ChatGLM3](https://github.com/THUDM/ChatGLM3) variant boasts a robust foundation, comprehensive functional support, and widespread availability for both academic and commercial uses. \n",
|
||||
"\n",
|
||||
"## Getting started\n",
|
||||
"### Installation\n",
|
||||
@@ -26,11 +28,11 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#!pip install --upgrade httpx httpx-sse PyJWT"
|
||||
"%pip install --quiet httpx[socks]==0.24.1 httpx-sse PyJWT"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -43,7 +45,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -61,13 +63,11 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"os.environ[\"ZHIPUAI_API_KEY\"] = \"zhipuai_api_key\""
|
||||
"zhipuai_api_key = \"your_api_key\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -80,11 +80,12 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"chat = ChatZhipuAI(\n",
|
||||
" api_key=zhipuai_api_key,\n",
|
||||
" model=\"glm-4\",\n",
|
||||
" temperature=0.5,\n",
|
||||
")"
|
||||
@@ -100,7 +101,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
@@ -115,9 +116,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\" Formed from bits and bytes,\\nA virtual mind takes flight,\\nConversing, learning fast,\\nEmpathy and wisdom sought.\"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"response = chat(messages)\n",
|
||||
"print(response.content) # Displays the AI-generated poem"
|
||||
@@ -134,7 +143,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -144,11 +153,12 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"streaming_chat = ChatZhipuAI(\n",
|
||||
" api_key=zhipuai_api_key,\n",
|
||||
" model=\"glm-4\",\n",
|
||||
" temperature=0.5,\n",
|
||||
" streaming=True,\n",
|
||||
@@ -158,9 +168,30 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" Formed from data's embrace,\n",
|
||||
"A digital soul to grace,\n",
|
||||
"AI, our trusted guide,\n",
|
||||
"Shaping minds, sides by side."
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"AIMessage(content=\" Formed from data's embrace,\\nA digital soul to grace,\\nAI, our trusted guide,\\nShaping minds, sides by side.\")"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"streaming_chat(messages)"
|
||||
]
|
||||
@@ -175,11 +206,12 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"async_chat = ChatZhipuAI(\n",
|
||||
" api_key=zhipuai_api_key,\n",
|
||||
" model=\"glm-4\",\n",
|
||||
" temperature=0.5,\n",
|
||||
")"
|
||||
@@ -187,11 +219,19 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 12,
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"generations=[[ChatGeneration(text=\" Formed from data's embrace,\\nA digital soul to grace,\\nAutomation's tender touch,\\nHarmony of man and machine.\", message=AIMessage(content=\" Formed from data's embrace,\\nA digital soul to grace,\\nAutomation's tender touch,\\nHarmony of man and machine.\"))]] llm_output={} run=[RunInfo(run_id=UUID('25fa687f-3961-4c63-b370-22f7647a4d42'))]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"response = await async_chat.agenerate([messages])\n",
|
||||
"print(response)"
|
||||
@@ -199,58 +239,47 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Using With Functions Call\n",
|
||||
"\n",
|
||||
"GLM-4 Model can be used with the function call as well,use the following code to run a simple LangChain json_chat_agent."
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
}
|
||||
"### Role Play Model\n",
|
||||
"Supports character role-playing based on personas, ultra-long multi-turn memory, and personalized dialogues for thousands of unique characters, widely applied in emotional companionship, game intelligent NPCs, virtual avatars for celebrities/stars/movie and TV IPs, digital humans/virtual anchors, text adventure games, and other anthropomorphic dialogue or gaming scenarios."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"os.environ[\"TAVILY_API_KEY\"] = \"tavily_api_key\""
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"execution_count": null
|
||||
"meta = {\n",
|
||||
" \"user_info\": \"My name is Lu Xingchen, a male, and a renowned director. I am also the collaborative director with Su Mengyuan. I specialize in directing movies with musical themes. Su Mengyuan respects me and regards me as a mentor and good friend.\",\n",
|
||||
" \"bot_info\": \"Su Mengyuan, whose real name is Su Yuanxin, is a popular domestic female singer and actress. She rose to fame quickly with her unique voice and exceptional stage presence after participating in a talent show, making her way into the entertainment industry. She is beautiful and charming, but her real allure lies in her talent and diligence. Su Mengyuan is a distinguished graduate of a music academy, skilled in songwriting, and has several popular original songs. Beyond her musical achievements, she is passionate about charity work, actively participating in public welfare activities, and spreading positive energy through her actions. In her work, she is very dedicated and immerses herself fully in her roles during filming, earning praise from industry professionals and love from fans. Despite being in the entertainment industry, she always maintains a low profile and a humble attitude, earning respect from her peers. In expression, Su Mengyuan likes to use 'we' and 'together,' emphasizing team spirit.\",\n",
|
||||
" \"bot_name\": \"Su Mengyuan\",\n",
|
||||
" \"user_name\": \"Lu Xingchen\",\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain import hub\n",
|
||||
"from langchain.agents import AgentExecutor, create_json_chat_agent\n",
|
||||
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
|
||||
"\n",
|
||||
"tools = [TavilySearchResults(max_results=1)]\n",
|
||||
"prompt = hub.pull(\"hwchase17/react-chat-json\")\n",
|
||||
"llm = ChatZhipuAI(temperature=0.01, model=\"glm-4\")\n",
|
||||
"\n",
|
||||
"agent = create_json_chat_agent(llm, tools, prompt)\n",
|
||||
"agent_executor = AgentExecutor(\n",
|
||||
" agent=agent, tools=tools, verbose=True, handle_parsing_errors=True\n",
|
||||
")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"execution_count": null
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"agent_executor.invoke({\"input\": \"what is LangChain?\"})"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"execution_count": null
|
||||
"messages = [\n",
|
||||
" AIMessage(\n",
|
||||
" content=\"(Narration: Su Mengyuan stars in a music-themed movie directed by Lu Xingchen. During filming, they have a disagreement over the performance of a particular scene.) Director, about this scene, I think we can try to start from the character's inner emotions to make the performance more authentic.\"\n",
|
||||
" ),\n",
|
||||
" HumanMessage(\n",
|
||||
" content=\"I understand your idea, but I believe that if we emphasize the inner emotions too much, it might overshadow the musical elements.\"\n",
|
||||
" ),\n",
|
||||
" AIMessage(\n",
|
||||
" content=\"Hmm, I understand. But the key to this scene is the character's emotional transformation. Could we try to express these emotions through music, so the audience can better feel the character's growth?\"\n",
|
||||
" ),\n",
|
||||
" HumanMessage(\n",
|
||||
" content=\"That sounds good. Let's try to combine the character's emotional transformation with the musical elements and see if we can achieve a better effect.\"\n",
|
||||
" ),\n",
|
||||
"]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
@@ -258,7 +258,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.adapters.openai import convert_messages_for_finetuning"
|
||||
"from langchain.adapters.openai import convert_messages_for_finetuning"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -173,7 +173,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.adapters.openai import convert_messages_for_finetuning"
|
||||
"from langchain.adapters.openai import convert_messages_for_finetuning"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -150,7 +150,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.adapters.openai import convert_messages_for_finetuning\n",
|
||||
"from langchain.adapters.openai import convert_messages_for_finetuning\n",
|
||||
"\n",
|
||||
"training_data = convert_messages_for_finetuning(chat_sessions)"
|
||||
]
|
||||
|
||||
@@ -285,7 +285,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.adapters.openai import convert_messages_for_finetuning\n",
|
||||
"from langchain.adapters.openai import convert_messages_for_finetuning\n",
|
||||
"\n",
|
||||
"training_data = convert_messages_for_finetuning(chat_sessions)"
|
||||
]
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
"source": [
|
||||
"import json\n",
|
||||
"\n",
|
||||
"from langchain_community.adapters.openai import convert_message_to_dict\n",
|
||||
"from langchain.adapters.openai import convert_message_to_dict\n",
|
||||
"from langchain_core.messages import AIMessage"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -166,7 +166,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.docstore.document import Document\n",
|
||||
"from langchain.docstore.document import Document\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def handle_record(record, id):\n",
|
||||
|
||||
@@ -149,7 +149,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.docstore.document import Document\n",
|
||||
"from langchain.docstore.document import Document\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def handle_record(record, id):\n",
|
||||
|
||||
@@ -151,7 +151,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.docstore.document import Document\n",
|
||||
"from langchain.docstore.document import Document\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def handle_record(record, id):\n",
|
||||
|
||||
@@ -156,7 +156,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.docstore.document import Document\n",
|
||||
"from langchain.docstore.document import Document\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def handle_record(record, id):\n",
|
||||
|
||||
@@ -152,7 +152,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.docstore.document import Document\n",
|
||||
"from langchain.docstore.document import Document\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def handle_record(record, id):\n",
|
||||
|
||||
@@ -149,7 +149,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.docstore.document import Document\n",
|
||||
"from langchain.docstore.document import Document\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def handle_record(record, id):\n",
|
||||
|
||||
@@ -152,7 +152,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.docstore.document import Document\n",
|
||||
"from langchain.docstore.document import Document\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def handle_record(record, id):\n",
|
||||
|
||||
@@ -153,7 +153,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.docstore.document import Document\n",
|
||||
"from langchain.docstore.document import Document\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def handle_record(record, id):\n",
|
||||
|
||||
@@ -100,8 +100,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.docstore.document import Document\n",
|
||||
"from langchain.indexes import VectorstoreIndexCreator\n",
|
||||
"from langchain_community.docstore.document import Document\n",
|
||||
"from langchain_community.document_loaders import ApifyDatasetLoader"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.docstore.document import Document"
|
||||
"from langchain.docstore.document import Document"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
BIN
docs/docs/integrations/document_loaders/example_data/fake.vsdx
Normal file
BIN
docs/docs/integrations/document_loaders/example_data/fake.vsdx
Normal file
Binary file not shown.
@@ -24,12 +24,12 @@
|
||||
"import os\n",
|
||||
"\n",
|
||||
"from langchain.indexes import VectorstoreIndexCreator\n",
|
||||
"from langchain_community.document_loaders.figma import FigmaFileLoader\n",
|
||||
"from langchain_core.prompts.chat import (\n",
|
||||
"from langchain.prompts.chat import (\n",
|
||||
" ChatPromptTemplate,\n",
|
||||
" HumanMessagePromptTemplate,\n",
|
||||
" SystemMessagePromptTemplate,\n",
|
||||
")\n",
|
||||
"from langchain_community.document_loaders.figma import FigmaFileLoader\n",
|
||||
"from langchain_openai import ChatOpenAI"
|
||||
]
|
||||
},
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,118 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "MwTWzDxYgbrR"
|
||||
},
|
||||
"source": [
|
||||
"# Glue Catalog\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The [AWS Glue Data Catalog](https://docs.aws.amazon.com/en_en/glue/latest/dg/catalog-and-crawler.html) is a centralized metadata repository that allows you to manage, access, and share metadata about your data stored in AWS. It acts as a metadata store for your data assets, enabling various AWS services and your applications to query and connect to the data they need efficiently.\n",
|
||||
"\n",
|
||||
"When you define data sources, transformations, and targets in AWS Glue, the metadata about these elements is stored in the Data Catalog. This includes information about data locations, schema definitions, runtime metrics, and more. It supports various data store types, such as Amazon S3, Amazon RDS, Amazon Redshift, and external databases compatible with JDBC. It is also directly integrated with Amazon Athena, Amazon Redshift Spectrum, and Amazon EMR, allowing these services to directly access and query the data.\n",
|
||||
"\n",
|
||||
"The Langchain GlueCatalogLoader will get the schema of all tables inside the given Glue database in the same format as Pandas dtype."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Setting up\n",
|
||||
"\n",
|
||||
"- Follow [instructions to set up an AWS accoung](https://docs.aws.amazon.com/athena/latest/ug/setting-up.html).\n",
|
||||
"- Install the boto3 library: `pip install boto3`\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Example"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "076NLjfngoWJ"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.document_loaders.glue_catalog import GlueCatalogLoader"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "XpMRQwU9gu44"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"database_name = \"my_database\"\n",
|
||||
"profile_name = \"my_profile\"\n",
|
||||
"\n",
|
||||
"loader = GlueCatalogLoader(\n",
|
||||
" database=database_name,\n",
|
||||
" profile_name=profile_name,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"schemas = loader.load()\n",
|
||||
"print(schemas)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Example with table filtering\n",
|
||||
"\n",
|
||||
"Table filtering allows you to selectively retrieve schema information for a specific subset of tables within a Glue database. Instead of loading the schemas for all tables, you can use the `table_filter` argument to specify exactly which tables you're interested in."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.document_loaders.glue_catalog import GlueCatalogLoader"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"database_name = \"my_database\"\n",
|
||||
"profile_name = \"my_profile\"\n",
|
||||
"table_filter = [\"table1\", \"table2\", \"table3\"]\n",
|
||||
"\n",
|
||||
"loader = GlueCatalogLoader(\n",
|
||||
" database=database_name, profile_name=profile_name, table_filter=table_filter\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"schemas = loader.load()\n",
|
||||
"print(schemas)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -322,52 +322,6 @@
|
||||
" print(doc.page_content.strip()[:60] + \"...\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7bde486a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Loading auth Identities\n",
|
||||
"\n",
|
||||
"Authorized identities for each file ingested by Google Drive Loader can be loaded along with metadata per Document."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e1d91045",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.document_loaders import GoogleDriveLoader\n",
|
||||
"\n",
|
||||
"loader = GoogleDriveLoader(\n",
|
||||
" folder_id=folder_id,\n",
|
||||
" load_auth=True,\n",
|
||||
" # Optional: configure whether to load authorized identities for each Document.\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"doc = loader.load()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "83557b75",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"You can pass load_auth=True, to add Google Drive document access identities to metadata."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "7ac1a43b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"doc[0].metadata"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cd13d7d1-db7a-498d-ac98-76ccd9ad9019",
|
||||
@@ -431,7 +385,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts.prompt import PromptTemplate\n",
|
||||
"from langchain.prompts.prompt import PromptTemplate\n",
|
||||
"\n",
|
||||
"loader = GoogleDriveLoader(\n",
|
||||
" folder_id=folder_id,\n",
|
||||
@@ -576,7 +530,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.5"
|
||||
"version": "3.10.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
"7. To find your `Tenant Name` follow the instructions at this [document](https://learn.microsoft.com/en-us/azure/active-directory-b2c/tenant-management-read-tenant-name). Once you got this, just remove `.onmicrosoft.com` from the value and hold the rest as your `Tenant Name`.\n",
|
||||
"8. To obtain your `Collection ID` and `Subsite ID`, you will need your **SharePoint** `site-name`. Your `SharePoint` site URL has the following format `https://<tenant-name>.sharepoint.com/sites/<site-name>`. The last part of this URL is the `site-name`.\n",
|
||||
"9. To Get the Site `Collection ID`, hit this URL in the browser: `https://<tenant>.sharepoint.com/sites/<site-name>/_api/site/id` and copy the value of the `Edm.Guid` property.\n",
|
||||
"10. To get the `Subsite ID` (or web ID) use: `https://<tenant>.sharepoint.com/sites/<site-name>/_api/web/id` and copy the value of the `Edm.Guid` property.\n",
|
||||
"10. To get the `Subsite ID` (or web ID) use: `https://<tenant>.sharepoint.com/<site-name>/_api/web/id` and copy the value of the `Edm.Guid` property.\n",
|
||||
"11. The `SharePoint site ID` has the following format: `<tenant-name>.sharepoint.com,<Collection ID>,<subsite ID>`. You can hold that value to use in the next step.\n",
|
||||
"12. Visit the [Graph Explorer Playground](https://developer.microsoft.com/en-us/graph/graph-explorer) to obtain your `Document Library ID`. The first step is to ensure you are logged in with the account associated with your **SharePoint** site. Then you need to make a request to `https://graph.microsoft.com/v1.0/sites/<SharePoint site ID>/drive` and the response will return a payload with a field `id` that holds the ID of your `Document Library ID`.\n",
|
||||
"\n",
|
||||
@@ -65,30 +65,6 @@
|
||||
"documents = loader.load()\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"If you are receiving the error `Resource not found for the segment`, try using the `folder_id` instead of the folder path, which can be obtained from the [Microsoft Graph API](https://developer.microsoft.com/en-us/graph/graph-explorer)\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
"loader = SharePointLoader(document_library_id=\"YOUR DOCUMENT LIBRARY ID\", auth_with_token=True\n",
|
||||
" folder_id=\"<folder-id>\")\n",
|
||||
"documents = loader.load()\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"If you wish to load documents from the root directory, you can omit `folder_id`, `folder_path` and `documents_ids` and loader will load root directory.\n",
|
||||
"```python\n",
|
||||
"# loads documents from root directory\n",
|
||||
"loader = SharePointLoader(document_library_id=\"YOUR DOCUMENT LIBRARY ID\", auth_with_token=True)\n",
|
||||
"documents = loader.load()\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"Combined with `recursive=True` you can simply load all documents from whole SharePoint:\n",
|
||||
"```python\n",
|
||||
"# loads documents from root directory\n",
|
||||
"loader = SharePointLoader(document_library_id=\"YOUR DOCUMENT LIBRARY ID\",\n",
|
||||
" recursive=True,\n",
|
||||
" auth_with_token=True)\n",
|
||||
"documents = loader.load()\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"#### 📑 Loading documents from a list of Documents IDs\n",
|
||||
"\n",
|
||||
"Another possibility is to provide a list of `object_id` for each document you want to load. For that, you will need to query the [Microsoft Graph API](https://developer.microsoft.com/en-us/graph/graph-explorer) to find all the documents ID that you are interested in. This [link](https://learn.microsoft.com/en-us/graph/api/resources/onedrive?view=graph-rest-1.0#commonly-accessed-resources) provides a list of endpoints that will be helpful to retrieve the documents ID.\n",
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
],
|
||||
"source": [
|
||||
"# Uncomment this to install psychicapi if you don't already have it installed\n",
|
||||
"!poetry run pip -q install psychicapi langchain-chroma"
|
||||
"!poetry run pip -q install psychicapi"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -78,7 +78,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import RetrievalQAWithSourcesChain\n",
|
||||
"from langchain_chroma import Chroma\n",
|
||||
"from langchain_community.vectorstores import Chroma\n",
|
||||
"from langchain_openai import OpenAI, OpenAIEmbeddings\n",
|
||||
"from langchain_text_splitters import CharacterTextSplitter"
|
||||
]
|
||||
|
||||
@@ -7,9 +7,7 @@
|
||||
"source": [
|
||||
"# WebBaseLoader\n",
|
||||
"\n",
|
||||
"This covers how to use `WebBaseLoader` to load all text from `HTML` webpages into a document format that we can use downstream. For more custom logic for loading webpages look at some child class examples such as `IMSDbLoader`, `AZLyricsLoader`, and `CollegeConfidentialLoader`. \n",
|
||||
"\n",
|
||||
"If you don't want to worry about website crawling, bypassing JS-blocking sites, and data cleaning, consider using `FireCrawlLoader`.\n"
|
||||
"This covers how to use `WebBaseLoader` to load all text from `HTML` webpages into a document format that we can use downstream. For more custom logic for loading webpages look at some child class examples such as `IMSDbLoader`, `AZLyricsLoader`, and `CollegeConfidentialLoader`"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -279,4 +277,4 @@
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
}
|
||||
@@ -175,7 +175,7 @@
|
||||
"source": [
|
||||
"## Uploading Hugging Face model to SageMaker endpoint\n",
|
||||
"\n",
|
||||
"Here is a sample `inference.py` for creating an endpoint that works with `SagemakerEndpointCrossEncoder`. For more details with step-by-step guidance, refer to [this article](https://huggingface.co/blog/kchoe/deploy-any-huggingface-model-to-sagemaker). \n",
|
||||
"Refer to [this article](https://www.philschmid.de/custom-inference-huggingface-sagemaker) for general guideline. Here is a simple `inference.py` for creating an endpoint that works with `SagemakerEndpointCrossEncoder`.\n",
|
||||
"\n",
|
||||
"It downloads Hugging Face model on the fly, so you do not need to keep the model artifacts such as `pytorch_model.bin` in your `model.tar.gz`."
|
||||
]
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"jupyter": {
|
||||
@@ -28,7 +28,42 @@
|
||||
"is_executing": true
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
|
||||
"To disable this warning, you can either:\n",
|
||||
"\t- Avoid using `tokenizers` before the fork if possible\n",
|
||||
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Note: you may need to restart the kernel to use updated packages.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
|
||||
"To disable this warning, you can either:\n",
|
||||
"\t- Avoid using `tokenizers` before the fork if possible\n",
|
||||
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Note: you may need to restart the kernel to use updated packages.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%pip install --upgrade-strategy eager \"optimum[openvino,nncf]\" --quiet\n",
|
||||
"%pip install --upgrade --quiet faiss-cpu"
|
||||
@@ -369,23 +404,46 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"jupyter": {
|
||||
"outputs_hidden": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Framework not specified. Using pt to export the model.\n",
|
||||
"Using the export variant default. Available variants are:\n",
|
||||
" - default: The default ONNX variant.\n",
|
||||
"Using framework PyTorch: 2.2.1+cu121\n",
|
||||
"Overriding 1 configuration item(s)\n",
|
||||
"\t- use_cache -> False\n",
|
||||
"/home/ethan/intel/langchain_test/lib/python3.10/site-packages/transformers/modeling_utils.py:4193: FutureWarning: `_is_quantized_training_enabled` is going to be deprecated in transformers 4.39.0. Please use `model.hf_quantizer.is_trainable` instead\n",
|
||||
" warnings.warn(\n",
|
||||
"Compiling the model to CPU ...\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[0, 16, 18, 6]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain.retrievers import ContextualCompressionRetriever\n",
|
||||
"from langchain_community.document_compressors.openvino_rerank import OpenVINOReranker\n",
|
||||
"\n",
|
||||
"model_name = \"BAAI/bge-reranker-large\"\n",
|
||||
"\n",
|
||||
"ov_compressor = OpenVINOReranker(model_name_or_path=model_name, top_n=4)\n",
|
||||
"compressor = OpenVINOReranker(model_name_or_path=model_name)\n",
|
||||
"compression_retriever = ContextualCompressionRetriever(\n",
|
||||
" base_compressor=ov_compressor, base_retriever=retriever\n",
|
||||
" base_compressor=compressor, base_retriever=retriever\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"compressed_docs = compression_retriever.get_relevant_documents(\n",
|
||||
@@ -403,7 +461,7 @@
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"After reranking, the top 4 documents are different from the top 4 documents retrieved by the base retriever."
|
||||
"After reranking, the top 3 documents are different from the top 3 documents retrieved by the base retriever."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -474,13 +532,37 @@
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Framework not specified. Using pt to export the model.\n",
|
||||
"Using the export variant default. Available variants are:\n",
|
||||
" - default: The default ONNX variant.\n",
|
||||
"Using framework PyTorch: 2.2.1+cu121\n",
|
||||
"Overriding 1 configuration item(s)\n",
|
||||
"\t- use_cache -> False\n",
|
||||
"/home/ethan/intel/langchain_test/lib/python3.10/site-packages/transformers/modeling_utils.py:4193: FutureWarning: `_is_quantized_training_enabled` is going to be deprecated in transformers 4.39.0. Please use `model.hf_quantizer.is_trainable` instead\n",
|
||||
" warnings.warn(\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from pathlib import Path\n",
|
||||
"\n",
|
||||
"ov_model_dir = \"bge-reranker-large-ov\"\n",
|
||||
"if not Path(ov_model_dir).exists():\n",
|
||||
" ov_compressor.save_model(ov_model_dir)"
|
||||
" from optimum.intel.openvino import OVModelForSequenceClassification\n",
|
||||
" from transformers import AutoTokenizer\n",
|
||||
"\n",
|
||||
" ov_model = OVModelForSequenceClassification.from_pretrained(\n",
|
||||
" model_name, compile=False, export=True\n",
|
||||
" )\n",
|
||||
" tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
|
||||
" ov_model.half()\n",
|
||||
" ov_model.save_pretrained(ov_model_dir)\n",
|
||||
" tokenizer.save_pretrained(ov_model_dir)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -497,7 +579,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"ov_compressor = OpenVINOReranker(model_name_or_path=ov_model_dir)"
|
||||
"compressor = OpenVINOReranker(model_name_or_path=ov_model_dir)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -512,7 +594,7 @@
|
||||
"\n",
|
||||
"* [OpenVINO Get Started Guide](https://www.intel.com/content/www/us/en/content-details/819067/openvino-get-started-guide.html).\n",
|
||||
"\n",
|
||||
"* [RAG Notebook with LangChain](https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/llm-rag-langchain)."
|
||||
"* [RAG Notebook with LangChain](https://github.com/openvinotoolkit/openvino_notebooks/blob/latest/notebooks/llm-chatbot/rag-chatbot.ipynb)."
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
@@ -118,7 +118,25 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip install --upgrade --quiet langchain langchain-community langchain-aws"
|
||||
"!pip install --upgrade --force-reinstall langchain"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip install --upgrade --force-reinstall langchain-core"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!pip install --upgrade --force-reinstall langchain-community"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -246,7 +264,7 @@
|
||||
"source": [
|
||||
"import boto3\n",
|
||||
"from langchain.chains.graph_qa.neptune_sparql import NeptuneSparqlQAChain\n",
|
||||
"from langchain_aws import ChatBedrock\n",
|
||||
"from langchain_community.chat_models import BedrockChat\n",
|
||||
"from langchain_community.graphs import NeptuneRdfGraph\n",
|
||||
"\n",
|
||||
"host = \"<your host>\"\n",
|
||||
@@ -261,7 +279,7 @@
|
||||
"\n",
|
||||
"MODEL_ID = \"anthropic.claude-v2\"\n",
|
||||
"bedrock_client = boto3.client(\"bedrock-runtime\")\n",
|
||||
"llm = ChatBedrock(model_id=MODEL_ID, client=bedrock_client)\n",
|
||||
"llm = BedrockChat(model_id=MODEL_ID, client=bedrock_client)\n",
|
||||
"\n",
|
||||
"chain = NeptuneSparqlQAChain.from_llm(\n",
|
||||
" llm=llm,\n",
|
||||
|
||||
@@ -79,8 +79,8 @@
|
||||
"\n",
|
||||
"from gqlalchemy import Memgraph\n",
|
||||
"from langchain.chains import GraphCypherQAChain\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_community.graphs import MemgraphGraph\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain_openai import ChatOpenAI"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -389,7 +389,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts.prompt import PromptTemplate\n",
|
||||
"from langchain.prompts.prompt import PromptTemplate\n",
|
||||
"\n",
|
||||
"CYPHER_GENERATION_TEMPLATE = \"\"\"Task:Generate Cypher statement to query a graph database.\n",
|
||||
"Instructions:\n",
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_community.llms.pai_eas_endpoint import PaiEasEndpoint\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"\n",
|
||||
"template = \"\"\"Question: {question}\n",
|
||||
"\n",
|
||||
|
||||
@@ -347,7 +347,7 @@
|
||||
"from langchain_core.messages import HumanMessage\n",
|
||||
"from langchain_google_vertexai import ChatVertexAI\n",
|
||||
"\n",
|
||||
"llm = ChatVertexAI(model_name=\"gemini-pro-vision\")\n",
|
||||
"llm = ChatVertexAI(model_name=\"gemini-ultra-vision\")\n",
|
||||
"\n",
|
||||
"image_message = {\n",
|
||||
" \"type\": \"image_url\",\n",
|
||||
|
||||
@@ -93,7 +93,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain_core.prompts import PromptTemplate"
|
||||
"from langchain.prompts import PromptTemplate"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -210,7 +210,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"\n",
|
||||
"template = \"Generate a random question about {topic}: Question: \"\n",
|
||||
"prompt = PromptTemplate.from_template(template)"
|
||||
|
||||
@@ -1633,7 +1633,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.docstore.document import Document\n",
|
||||
"from langchain.docstore.document import Document\n",
|
||||
"\n",
|
||||
"docs = [Document(page_content=t) for t in texts[:3]]\n",
|
||||
"from langchain.chains.summarize import load_summarize_chain"
|
||||
|
||||
@@ -103,7 +103,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"\n",
|
||||
"template = \"\"\"Question: {question}\n",
|
||||
"\n",
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
" \n",
|
||||
"2. Paste your API key in in the code cell below.\n",
|
||||
"\n",
|
||||
"Note: If you want to use a different LLM model, you can containerize the model and make a custom OctoAI endpoint yourself, by following [Build a Container from Python](https://octo.ai/docs/bring-your-own-model/advanced-build-a-container-from-scratch-in-python) and [Create a Custom Endpoint from a Container](https://octo.ai/docs/bring-your-own-model/create-custom-endpoints-from-a-container/create-custom-endpoints-from-a-container) and then updating your `OCTOAI_API_BASE` environment variable.\n"
|
||||
"Note: If you want to use a different LLM model, you can containerize the model and make a custom OctoAI endpoint yourself, by following [Build a Container from Python](https://octo.ai/docs/bring-your-own-model/advanced-build-a-container-from-scratch-in-python) and [Create a Custom Endpoint from a Container](https://octo.ai/docs/bring-your-own-model/create-custom-endpoints-from-a-container/create-custom-endpoints-from-a-container) and then update your Endpoint URL in the code cell below.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -29,7 +29,8 @@
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"os.environ[\"OCTOAI_API_TOKEN\"] = \"OCTOAI_API_TOKEN\""
|
||||
"os.environ[\"OCTOAI_API_TOKEN\"] = \"OCTOAI_API_TOKEN\"\n",
|
||||
"os.environ[\"ENDPOINT_URL\"] = \"https://text.octoai.run/v1/chat/completions\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -67,33 +68,44 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = OctoAIEndpoint(\n",
|
||||
" model=\"llama-2-13b-chat-fp16\",\n",
|
||||
" max_tokens=200,\n",
|
||||
" presence_penalty=0,\n",
|
||||
" temperature=0.1,\n",
|
||||
" top_p=0.9,\n",
|
||||
" model_kwargs={\n",
|
||||
" \"model\": \"llama-2-13b-chat-fp16\",\n",
|
||||
" \"max_tokens\": 128,\n",
|
||||
" \"presence_penalty\": 0,\n",
|
||||
" \"temperature\": 0.1,\n",
|
||||
" \"top_p\": 0.9,\n",
|
||||
" \"messages\": [\n",
|
||||
" {\n",
|
||||
" \"role\": \"system\",\n",
|
||||
" \"content\": \"You are a helpful assistant. Keep your responses limited to one short paragraph if possible.\",\n",
|
||||
" },\n",
|
||||
" ],\n",
|
||||
" },\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" Sure thing! Here's my response:\n",
|
||||
"\n",
|
||||
"Leonardo da Vinci was a true Renaissance man - an Italian polymath who excelled in various fields, including painting, sculpture, engineering, mathematics, anatomy, and geology. He is widely considered one of the greatest painters of all time, and his inventive and innovative works continue to inspire and influence artists and thinkers to this day. Some of his most famous works include the Mona Lisa, The Last Supper, and Vitruvian Man. \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"question = \"Who was Leonardo da Vinci?\"\n",
|
||||
"question = \"Who was leonardo davinci?\"\n",
|
||||
"\n",
|
||||
"llm_chain = LLMChain(prompt=prompt, llm=llm)\n",
|
||||
"\n",
|
||||
"print(llm_chain.run(question))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Leonardo da Vinci was a true Renaissance man. He was born in 1452 in Vinci, Italy and was known for his work in various fields, including art, science, engineering, and mathematics. He is considered one of the greatest painters of all time, and his most famous works include the Mona Lisa and The Last Supper. In addition to his art, da Vinci made significant contributions to engineering and anatomy, and his designs for machines and inventions were centuries ahead of his time. He is also known for his extensive journals and drawings, which provide valuable insights into his thoughts and ideas. Da Vinci's legacy continues to inspire and influence artists, scientists, and thinkers around the world today."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
"* [Download](https://ollama.ai/download) and install Ollama onto the available supported platforms (including Windows Subsystem for Linux)\n",
|
||||
"* Fetch available LLM model via `ollama pull <name-of-model>`\n",
|
||||
" * View a list of available models via the [model library](https://ollama.ai/library)\n",
|
||||
" * e.g., `ollama pull llama3`\n",
|
||||
" * e.g., for `Llama-7b`: `ollama pull llama2`\n",
|
||||
"* This will download the default tagged version of the model. Typically, the default points to the latest, smallest sized-parameter model.\n",
|
||||
"\n",
|
||||
"> On Mac, the models will be download to `~/.ollama/models`\n",
|
||||
@@ -37,7 +37,7 @@
|
||||
"\n",
|
||||
"You can see a full list of supported parameters on the [API reference page](https://api.python.langchain.com/en/latest/llms/langchain.llms.ollama.Ollama.html).\n",
|
||||
"\n",
|
||||
"If you are using a LLaMA `chat` model (e.g., `ollama pull llama3`) then you can use the `ChatOllama` interface.\n",
|
||||
"If you are using a LLaMA `chat` model (e.g., `ollama pull llama2:7b-chat`) then you can use the `ChatOllama` interface.\n",
|
||||
"\n",
|
||||
"This includes [special tokens](https://huggingface.co/blog/llama2#how-to-prompt-llama-2) for system message and user input.\n",
|
||||
"\n",
|
||||
@@ -56,7 +56,7 @@
|
||||
"\n",
|
||||
"```bash\n",
|
||||
"curl http://localhost:11434/api/generate -d '{\n",
|
||||
" \"model\": \"llama3\",\n",
|
||||
" \"model\": \"llama2\",\n",
|
||||
" \"prompt\":\"Why is the sky blue?\"\n",
|
||||
"}'\n",
|
||||
"```\n",
|
||||
@@ -70,16 +70,16 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"\"Here's one:\\n\\nWhy don't scientists trust atoms?\\n\\nBecause they make up everything!\\n\\nHope that made you smile! Do you want to hear another one?\""
|
||||
"\"Sure! Here's a quick one:\\n\\nWhy don't scientists trust atoms?\\nBecause they make up everything!\\n\\nI hope that brought a smile to your face!\""
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -87,7 +87,7 @@
|
||||
"source": [
|
||||
"from langchain_community.llms import Ollama\n",
|
||||
"\n",
|
||||
"llm = Ollama(model=\"llama3\")\n",
|
||||
"llm = Ollama(model=\"llama2\")\n",
|
||||
"\n",
|
||||
"llm.invoke(\"Tell me a joke\")"
|
||||
]
|
||||
@@ -298,7 +298,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.8"
|
||||
"version": "3.9.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -90,7 +90,7 @@
|
||||
"device = \"CPU\"\n",
|
||||
"tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
|
||||
"ov_model = OVModelForCausalLM.from_pretrained(\n",
|
||||
" model_id, export=True, device=device, ov_config=ov_config\n",
|
||||
" model_id, device=device, ov_config=ov_config\n",
|
||||
")\n",
|
||||
"ov_pipe = pipeline(\n",
|
||||
" \"text-generation\", model=ov_model, tokenizer=tokenizer, max_new_tokens=10\n",
|
||||
@@ -116,7 +116,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"\n",
|
||||
"template = \"\"\"Question: {question}\n",
|
||||
"\n",
|
||||
@@ -185,11 +185,11 @@
|
||||
" pipeline_kwargs={\"max_new_tokens\": 10},\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"chain = prompt | ov_llm\n",
|
||||
"ov_chain = prompt | ov_llm\n",
|
||||
"\n",
|
||||
"question = \"What is electroencephalography?\"\n",
|
||||
"\n",
|
||||
"print(chain.invoke({\"question\": question}))"
|
||||
"print(ov_chain.invoke({\"question\": question}))"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -229,7 +229,7 @@
|
||||
"\n",
|
||||
"* [OpenVINO Get Started Guide](https://www.intel.com/content/www/us/en/content-details/819067/openvino-get-started-guide.html).\n",
|
||||
" \n",
|
||||
"* [RAG Notebook with LangChain](https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/llm-rag-langchain)."
|
||||
"* [RAG Notebook with LangChain](https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/llm-chatbot)."
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 7,
|
||||
"metadata": {
|
||||
"scrolled": true,
|
||||
"tags": []
|
||||
@@ -49,21 +49,17 @@
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Collecting replicate\n",
|
||||
" Using cached replicate-0.25.1-py3-none-any.whl.metadata (24 kB)\n",
|
||||
"Requirement already satisfied: httpx<1,>=0.21.0 in /Users/charlieholtz/miniconda3/envs/langchain/lib/python3.9/site-packages (from replicate) (0.24.1)\n",
|
||||
"Requirement already satisfied: packaging in /Users/charlieholtz/miniconda3/envs/langchain/lib/python3.9/site-packages (from replicate) (23.2)\n",
|
||||
"Requirement already satisfied: pydantic>1.10.7 in /Users/charlieholtz/miniconda3/envs/langchain/lib/python3.9/site-packages (from replicate) (1.10.14)\n",
|
||||
"Requirement already satisfied: typing-extensions>=4.5.0 in /Users/charlieholtz/miniconda3/envs/langchain/lib/python3.9/site-packages (from replicate) (4.10.0)\n",
|
||||
"Requirement already satisfied: certifi in /Users/charlieholtz/miniconda3/envs/langchain/lib/python3.9/site-packages (from httpx<1,>=0.21.0->replicate) (2024.2.2)\n",
|
||||
"Requirement already satisfied: httpcore<0.18.0,>=0.15.0 in /Users/charlieholtz/miniconda3/envs/langchain/lib/python3.9/site-packages (from httpx<1,>=0.21.0->replicate) (0.17.3)\n",
|
||||
"Requirement already satisfied: idna in /Users/charlieholtz/miniconda3/envs/langchain/lib/python3.9/site-packages (from httpx<1,>=0.21.0->replicate) (3.6)\n",
|
||||
"Requirement already satisfied: sniffio in /Users/charlieholtz/miniconda3/envs/langchain/lib/python3.9/site-packages (from httpx<1,>=0.21.0->replicate) (1.3.1)\n",
|
||||
"Requirement already satisfied: h11<0.15,>=0.13 in /Users/charlieholtz/miniconda3/envs/langchain/lib/python3.9/site-packages (from httpcore<0.18.0,>=0.15.0->httpx<1,>=0.21.0->replicate) (0.14.0)\n",
|
||||
"Requirement already satisfied: anyio<5.0,>=3.0 in /Users/charlieholtz/miniconda3/envs/langchain/lib/python3.9/site-packages (from httpcore<0.18.0,>=0.15.0->httpx<1,>=0.21.0->replicate) (3.7.1)\n",
|
||||
"Requirement already satisfied: exceptiongroup in /Users/charlieholtz/miniconda3/envs/langchain/lib/python3.9/site-packages (from anyio<5.0,>=3.0->httpcore<0.18.0,>=0.15.0->httpx<1,>=0.21.0->replicate) (1.2.0)\n",
|
||||
"Using cached replicate-0.25.1-py3-none-any.whl (39 kB)\n",
|
||||
" Using cached replicate-0.9.0-py3-none-any.whl (21 kB)\n",
|
||||
"Requirement already satisfied: packaging in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from replicate) (23.1)\n",
|
||||
"Requirement already satisfied: pydantic>1 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from replicate) (1.10.9)\n",
|
||||
"Requirement already satisfied: requests>2 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from replicate) (2.28.2)\n",
|
||||
"Requirement already satisfied: typing-extensions>=4.2.0 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from pydantic>1->replicate) (4.5.0)\n",
|
||||
"Requirement already satisfied: charset-normalizer<4,>=2 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from requests>2->replicate) (3.1.0)\n",
|
||||
"Requirement already satisfied: idna<4,>=2.5 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from requests>2->replicate) (3.4)\n",
|
||||
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from requests>2->replicate) (1.26.16)\n",
|
||||
"Requirement already satisfied: certifi>=2017.4.17 in /root/Source/github/docugami.langchain/libs/langchain/.venv/lib/python3.9/site-packages (from requests>2->replicate) (2023.5.7)\n",
|
||||
"Installing collected packages: replicate\n",
|
||||
"Successfully installed replicate-0.25.1\n"
|
||||
"Successfully installed replicate-0.9.0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -73,7 +69,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
@@ -88,7 +84,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
@@ -101,7 +97,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 56,
|
||||
"execution_count": 18,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
@@ -120,28 +116,28 @@
|
||||
"\n",
|
||||
"Find a model on the [replicate explore page](https://replicate.com/explore), and then paste in the model name and version in this format: model_name/version.\n",
|
||||
"\n",
|
||||
"For example, here is [`Meta Llama 3`](https://replicate.com/meta/meta-llama-3-8b-instruct)."
|
||||
"For example, here is [`LLama-V2`](https://replicate.com/a16z-infra/llama13b-v2-chat)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 58,
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"\"Let's break this down step by step:\\n\\n1. A dog is a living being, specifically a mammal.\\n2. Dogs do not possess the cognitive abilities or physical characteristics necessary to operate a vehicle, such as a car.\\n3. Operating a car requires complex mental and physical abilities, including:\\n\\t* Understanding of traffic laws and rules\\n\\t* Ability to read and comprehend road signs\\n\\t* Ability to make decisions quickly and accurately\\n\\t* Ability to physically manipulate the vehicle's controls (e.g., steering wheel, pedals)\\n4. Dogs do not possess any of these abilities. They are unable to read or comprehend written language, let alone complex traffic laws.\\n5. Dogs also lack the physical dexterity and coordination to operate a vehicle's controls. Their paws and claws are not adapted for grasping or manipulating small, precise objects like a steering wheel or pedals.\\n6. Therefore, it is not possible for a dog to drive a car.\\n\\nAnswer: No.\""
|
||||
"'1. Dogs do not have the ability to operate complex machinery like cars.\\n2. Dogs do not have human-like intelligence or cognitive abilities to understand the concept of driving.\\n3. Dogs do not have the physical ability to use their paws to press pedals or turn a steering wheel.\\n4. Therefore, a dog cannot drive a car.'"
|
||||
]
|
||||
},
|
||||
"execution_count": 58,
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"llm = Replicate(\n",
|
||||
" model=\"meta/meta-llama-3-8b-instruct\",\n",
|
||||
" model=\"a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5\",\n",
|
||||
" model_kwargs={\"temperature\": 0.75, \"max_length\": 500, \"top_p\": 1},\n",
|
||||
")\n",
|
||||
"prompt = \"\"\"\n",
|
||||
@@ -199,7 +195,7 @@
|
||||
],
|
||||
"source": [
|
||||
"prompt = \"\"\"\n",
|
||||
"Answer the following yes/no question by reasoning step by step.\n",
|
||||
"Answer the following yes/no question by reasoning step by step. \n",
|
||||
"Can a dog drive a car?\n",
|
||||
"\"\"\"\n",
|
||||
"llm(prompt)"
|
||||
@@ -558,7 +554,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.19"
|
||||
"version": "3.9.1"
|
||||
},
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
|
||||
@@ -58,7 +58,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.docstore.document import Document"
|
||||
"from langchain.docstore.document import Document"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1,19 +1,30 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Solar\n",
|
||||
"\n",
|
||||
"*This community integration is deprecated. You should use [`ChatUpstage`](../../chat/upstage) instead to access Solar LLM via the chat model connector.*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"execution_count": 1,
|
||||
"id": "2ff00e23-1a90-4a39-b220-83ebfffd96d6",
|
||||
"metadata": {
|
||||
"execution": {
|
||||
"iopub.execute_input": "2024-03-06T17:10:57.375714Z",
|
||||
"iopub.status.busy": "2024-03-06T17:10:57.375261Z",
|
||||
"iopub.status.idle": "2024-03-06T17:11:03.473978Z",
|
||||
"shell.execute_reply": "2024-03-06T17:11:03.472875Z",
|
||||
"shell.execute_reply.started": "2024-03-06T17:10:57.375670Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"\"Once upon a time, in a far-off land, there was a young girl named Lily. Lily was a kind and curious girl who loved to explore the world around her. One day, while wandering through the forest, she came across a small, shimmering pond.\\n\\nAs she approached the pond, she saw a beautiful, glowing flower floating on the water's surface. Lily reached out to touch the flower, and as she did, she felt a strange tingling sensation. Suddenly, the flower began to glow even brighter, and Lily was transported to a magical world filled with talking animals and enchanted forests.\\n\\nIn this world, Lily met a wise old owl named Winston who told her that the flower she had touched was a magical one that could grant her any wish she desired. Lily was overjoyed and asked Winston to show her around the magical world.\\n\\nTogether, they explored the enchanted forests, met friendly animals, and discovered hidden treasures. Lily was having the time of her life, but she knew that she couldn't stay in this magical world forever. Eventually, she had to return home.\\n\\nAs she said goodbye to Winston and the magical world, Lily realized that she had learned an important lesson. She had discovered that sometimes, the most magical things in life are the ones that are right in front of us, if we only take the time to look.\\n\\nFrom that day on, Lily always kept her eyes open for the magic in the world around her, and she never forgot the adventure she had in the enchanted forest.\""
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
@@ -26,13 +37,41 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"execution_count": 2,
|
||||
"id": "67fa1711-f08f-43fa-a3bd-75ae5bc6b988",
|
||||
"metadata": {
|
||||
"execution": {
|
||||
"iopub.execute_input": "2024-03-06T17:11:11.359924Z",
|
||||
"iopub.status.busy": "2024-03-06T17:11:11.358357Z",
|
||||
"iopub.status.idle": "2024-03-06T17:11:16.692138Z",
|
||||
"shell.execute_reply": "2024-03-06T17:11:16.686492Z",
|
||||
"shell.execute_reply.started": "2024-03-06T17:11:11.359835Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/Users/ary/dev/llm/langchain/libs/core/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n",
|
||||
" warn_deprecated(\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Step 1: Determine the year Justin Bieber was born.\\nJustin Bieber was born on March 1, 1994.\\n\\nStep 2: Determine the Super Bowl held in 1994.\\nSuper Bowl XXVIII was held in 1994.\\n\\nStep 3: Determine the winning team of Super Bowl XXVIII.\\nThe Dallas Cowboys won Super Bowl XXVIII in 1994.\\n\\nFinal Answer: The Dallas Cowboys won the Super Bowl in the year Justin Bieber was born (1994).'"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_community.llms.solar import Solar\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"\n",
|
||||
"template = \"\"\"Question: {question}\n",
|
||||
"\n",
|
||||
@@ -47,13 +86,35 @@
|
||||
"\n",
|
||||
"llm_chain.run(question)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "91961983-d0d5-4901-b854-531e158c0416",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
||||
@@ -1,15 +1,75 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Titan Takeoff\n",
|
||||
"\n",
|
||||
"`TitanML` helps businesses build and deploy better, smaller, cheaper, and faster NLP models through our training, compression, and inference optimization platform.\n",
|
||||
">`TitanML` helps businesses build and deploy better, smaller, cheaper, and faster NLP models through our training, compression, and inference optimization platform. \n",
|
||||
"\n",
|
||||
"Our inference server, [Titan Takeoff](https://docs.titanml.co/docs/intro) enables deployment of LLMs locally on your hardware in a single command. Most generative model architectures are supported, such as Falcon, Llama 2, GPT2, T5 and many more. If you experience trouble with a specific model, please let us know at hello@titanml.co."
|
||||
">Our inference server, [Titan Takeoff](https://docs.titanml.co/docs/titan-takeoff/getting-started) enables deployment of LLMs locally on your hardware in a single command. Most generative model architectures are supported, such as Falcon, Llama 2, GPT2, T5 and many more."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"To get started with Iris Takeoff, all you need is to have docker and python installed on your local system. If you wish to use the server with gpu support, then you will need to install docker with cuda support.\n",
|
||||
"\n",
|
||||
"For Mac and Windows users, make sure you have the docker daemon running! You can check this by running docker ps in your terminal. To start the daemon, open the docker desktop app.\n",
|
||||
"\n",
|
||||
"Run the following command to install the Iris CLI that will enable you to run the takeoff server:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"vscode": {
|
||||
"languageId": "shellscript"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%pip install --upgrade --quiet titan-iris"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Choose a Model\n",
|
||||
"Takeoff supports many of the most powerful generative text models, such as Falcon, MPT, and Llama. See the [supported models](https://docs.titanml.co/docs/titan-takeoff/supported-models) for more information. For information about using your own models, see the [custom models](https://docs.titanml.co/docs/titan-takeoff/Advanced/custom-models).\n",
|
||||
"\n",
|
||||
"Going forward in this demo we will be using the falcon 7B instruct model. This is a good open-source model that is trained to follow instructions, and is small enough to easily inference even on CPUs.\n",
|
||||
"\n",
|
||||
"## Taking off\n",
|
||||
"Models are referred to by their model id on HuggingFace. Takeoff uses port 8000 by default, but can be configured to use another port. There is also support to use a Nvidia GPU by specifying cuda for the device flag.\n",
|
||||
"\n",
|
||||
"To start the takeoff server, run:\n",
|
||||
"\n",
|
||||
"```shell\n",
|
||||
"iris takeoff --model tiiuae/falcon-7b-instruct --device cpu\n",
|
||||
"iris takeoff --model tiiuae/falcon-7b-instruct --device cuda # Nvidia GPU required\n",
|
||||
"iris takeoff --model tiiuae/falcon-7b-instruct --device cpu --port 5000 # run on port 5000 (default: 8000)\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"You will then be directed to a login page, where you will need to create an account to proceed.\n",
|
||||
"After logging in, run the command onscreen to check whether the server is ready. When it is ready, you can start using the Takeoff integration.\n",
|
||||
"\n",
|
||||
"To shutdown the server, run the following command. You will be presented with options on which Takeoff server to shut down, in case you have multiple running servers.\n",
|
||||
"\n",
|
||||
"```shell\n",
|
||||
"iris takeoff --shutdown # shutdown the server\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -17,8 +77,8 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Example usage\n",
|
||||
"Here are some helpful examples to get started using Titan Takeoff Server. You need to make sure Takeoff Server has been started in the background before running these commands. For more information see [docs page for launching Takeoff](https://docs.titanml.co/docs/Docs/launching/)."
|
||||
"## Inferencing your model\n",
|
||||
"To access your LLM, use the TitanTakeoff LLM wrapper:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -27,119 +87,50 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import time\n",
|
||||
"from langchain_community.llms import TitanTakeoff\n",
|
||||
"\n",
|
||||
"llm = TitanTakeoff(\n",
|
||||
" base_url=\"http://localhost:8000\", generate_max_length=128, temperature=1.0\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"prompt = \"What is the largest planet in the solar system?\"\n",
|
||||
"\n",
|
||||
"llm(prompt)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"No parameters are needed by default, but a baseURL that points to your desired URL where Takeoff is running can be specified and [generation parameters](https://docs.titanml.co/docs/titan-takeoff/Advanced/generation-parameters) can be supplied.\n",
|
||||
"\n",
|
||||
"### Streaming\n",
|
||||
"Streaming is also supported via the streaming flag:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.callbacks.manager import CallbackManager\n",
|
||||
"from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n",
|
||||
"\n",
|
||||
"# Note importing TitanTakeoffPro instead of TitanTakeoff will work as well both use same object under the hood\n",
|
||||
"from langchain_community.llms import TitanTakeoff\n",
|
||||
"from langchain_core.prompts import PromptTemplate"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Example 1\n",
|
||||
"\n",
|
||||
"Basic use assuming Takeoff is running on your machine using its default ports (ie localhost:3000).\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = TitanTakeoff()\n",
|
||||
"output = llm.invoke(\"What is the weather in London in August?\")\n",
|
||||
"print(output)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Example 2\n",
|
||||
"\n",
|
||||
"Specifying a port and other generation parameters"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = TitanTakeoff(port=3000)\n",
|
||||
"# A comprehensive list of parameters can be found at https://docs.titanml.co/docs/next/apis/Takeoff%20inference_REST_API/generate#request\n",
|
||||
"output = llm.invoke(\n",
|
||||
" \"What is the largest rainforest in the world?\",\n",
|
||||
" consumer_group=\"primary\",\n",
|
||||
" min_new_tokens=128,\n",
|
||||
" max_new_tokens=512,\n",
|
||||
" no_repeat_ngram_size=2,\n",
|
||||
" sampling_topk=1,\n",
|
||||
" sampling_topp=1.0,\n",
|
||||
" sampling_temperature=1.0,\n",
|
||||
" repetition_penalty=1.0,\n",
|
||||
" regex_string=\"\",\n",
|
||||
" json_schema=None,\n",
|
||||
")\n",
|
||||
"print(output)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Example 3\n",
|
||||
"\n",
|
||||
"Using generate for multiple inputs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = TitanTakeoff()\n",
|
||||
"rich_output = llm.generate([\"What is Deep Learning?\", \"What is Machine Learning?\"])\n",
|
||||
"print(rich_output.generations)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Example 4\n",
|
||||
"\n",
|
||||
"Streaming output"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"llm = TitanTakeoff(\n",
|
||||
" streaming=True, callback_manager=CallbackManager([StreamingStdOutCallbackHandler()])\n",
|
||||
" callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), streaming=True\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"prompt = \"What is the capital of France?\"\n",
|
||||
"output = llm.invoke(prompt)\n",
|
||||
"print(output)"
|
||||
"\n",
|
||||
"llm(prompt)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Example 5\n",
|
||||
"\n",
|
||||
"Using LCEL"
|
||||
"### Integration with LLMChain"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -148,48 +139,19 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"\n",
|
||||
"llm = TitanTakeoff()\n",
|
||||
"prompt = PromptTemplate.from_template(\"Tell me about {topic}\")\n",
|
||||
"chain = prompt | llm\n",
|
||||
"output = chain.invoke({\"topic\": \"the universe\"})\n",
|
||||
"print(output)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Example 6\n",
|
||||
"\n",
|
||||
"Starting readers using TitanTakeoff Python Wrapper. If you haven't created any readers with first launching Takeoff, or you want to add another you can do so when you initialize the TitanTakeoff object. Just pass a list of model configs you want to start as the `models` parameter."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Model config for the llama model, where you can specify the following parameters:\n",
|
||||
"# model_name (str): The name of the model to use\n",
|
||||
"# device: (str): The device to use for inference, cuda or cpu\n",
|
||||
"# consumer_group (str): The consumer group to place the reader into\n",
|
||||
"# tensor_parallel (Optional[int]): The number of gpus you would like your model to be split across\n",
|
||||
"# max_seq_length (int): The maximum sequence length to use for inference, defaults to 512\n",
|
||||
"# max_batch_size (int_: The max batch size for continuous batching of requests\n",
|
||||
"llama_model = {\n",
|
||||
" \"model_name\": \"TheBloke/Llama-2-7b-Chat-AWQ\",\n",
|
||||
" \"device\": \"cuda\",\n",
|
||||
" \"consumer_group\": \"llama\",\n",
|
||||
"}\n",
|
||||
"llm = TitanTakeoff(models=[llama_model])\n",
|
||||
"template = \"What is the capital of {country}\"\n",
|
||||
"\n",
|
||||
"# The model needs time to spin up, length of time need will depend on the size of model and your network connection speed\n",
|
||||
"time.sleep(60)\n",
|
||||
"prompt = PromptTemplate.from_template(template)\n",
|
||||
"\n",
|
||||
"prompt = \"What is the capital of France?\"\n",
|
||||
"output = llm.invoke(prompt, consumer_group=\"llama\")\n",
|
||||
"print(output)"
|
||||
"llm_chain = LLMChain(llm=llm, prompt=prompt)\n",
|
||||
"\n",
|
||||
"generated = llm_chain.run(country=\"Belgium\")\n",
|
||||
"print(generated)"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
102
docs/docs/integrations/llms/titan_takeoff_pro.ipynb
Normal file
102
docs/docs/integrations/llms/titan_takeoff_pro.ipynb
Normal file
@@ -0,0 +1,102 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Titan Takeoff Pro\n",
|
||||
"\n",
|
||||
"`TitanML` helps businesses build and deploy better, smaller, cheaper, and faster NLP models through our training, compression, and inference optimization platform.\n",
|
||||
"\n",
|
||||
">Note: These docs are for the Pro version of Titan Takeoff. For the community version, see the page for Titan Takeoff.\n",
|
||||
"\n",
|
||||
"Our inference server, [Titan Takeoff (Pro Version)](https://docs.titanml.co/docs/titan-takeoff/pro-features/feature-comparison) enables deployment of LLMs locally on your hardware in a single command. Most generative model architectures are supported, such as Falcon, Llama 2, GPT2, T5 and many more."
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Example usage\n",
|
||||
"Here are some helpful examples to get started using the Pro version of Titan Takeoff Server.\n",
|
||||
"No parameters are needed by default, but a baseURL that points to your desired URL where Takeoff is running can be specified and generation parameters can be supplied."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.callbacks.manager import CallbackManager\n",
|
||||
"from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n",
|
||||
"from langchain_community.llms import TitanTakeoffPro\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"\n",
|
||||
"# Example 1: Basic use\n",
|
||||
"llm = TitanTakeoffPro()\n",
|
||||
"output = llm(\"What is the weather in London in August?\")\n",
|
||||
"print(output)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Example 2: Specifying a port and other generation parameters\n",
|
||||
"llm = TitanTakeoffPro(\n",
|
||||
" base_url=\"http://localhost:3000\",\n",
|
||||
" min_new_tokens=128,\n",
|
||||
" max_new_tokens=512,\n",
|
||||
" no_repeat_ngram_size=2,\n",
|
||||
" sampling_topk=1,\n",
|
||||
" sampling_topp=1.0,\n",
|
||||
" sampling_temperature=1.0,\n",
|
||||
" repetition_penalty=1.0,\n",
|
||||
" regex_string=\"\",\n",
|
||||
")\n",
|
||||
"output = llm(\"What is the largest rainforest in the world?\")\n",
|
||||
"print(output)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Example 3: Using generate for multiple inputs\n",
|
||||
"llm = TitanTakeoffPro()\n",
|
||||
"rich_output = llm.generate([\"What is Deep Learning?\", \"What is Machine Learning?\"])\n",
|
||||
"print(rich_output.generations)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Example 4: Streaming output\n",
|
||||
"llm = TitanTakeoffPro(\n",
|
||||
" streaming=True, callback_manager=CallbackManager([StreamingStdOutCallbackHandler()])\n",
|
||||
")\n",
|
||||
"prompt = \"What is the capital of France?\"\n",
|
||||
"llm(prompt)\n",
|
||||
"\n",
|
||||
"# Example 5: Using LCEL\n",
|
||||
"llm = TitanTakeoffPro()\n",
|
||||
"prompt = PromptTemplate.from_template(\"Tell me about {topic}\")\n",
|
||||
"chain = prompt | llm\n",
|
||||
"chain.invoke({\"topic\": \"the universe\"})"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.10.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
@@ -115,7 +115,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"\n",
|
||||
"template = \"\"\"Question: {question}\n",
|
||||
"\n",
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_openai import OpenAI\n",
|
||||
"\n",
|
||||
"template = \"\"\"You are a chatbot having a conversation with a human.\n",
|
||||
|
||||
@@ -2,28 +2,6 @@
|
||||
|
||||
The `LangChain` integrations related to [Amazon AWS](https://aws.amazon.com/) platform.
|
||||
|
||||
First-party AWS integrations are available in the `langchain_aws` package.
|
||||
|
||||
```bash
|
||||
pip install langchain-aws
|
||||
```
|
||||
|
||||
And there are also some community integrations available in the `langchain_community` package with the `boto3` optional dependency.
|
||||
|
||||
```bash
|
||||
pip install langchain-community boto3
|
||||
```
|
||||
|
||||
## Chat models
|
||||
|
||||
### Bedrock Chat
|
||||
|
||||
See a [usage example](/docs/integrations/chat/bedrock).
|
||||
|
||||
```python
|
||||
from langchain_aws import ChatBedrock
|
||||
```
|
||||
|
||||
## LLMs
|
||||
|
||||
### Bedrock
|
||||
@@ -42,7 +20,7 @@ from langchain_aws import ChatBedrock
|
||||
See a [usage example](/docs/integrations/llms/bedrock).
|
||||
|
||||
```python
|
||||
from langchain_aws import BedrockLLM
|
||||
from langchain_community.llms.bedrock import Bedrock
|
||||
```
|
||||
|
||||
### Amazon API Gateway
|
||||
@@ -75,7 +53,18 @@ We use `SageMaker` to host our model and expose it as the `SageMaker Endpoint`.
|
||||
See a [usage example](/docs/integrations/llms/sagemaker).
|
||||
|
||||
```python
|
||||
from langchain_aws import SagemakerEndpoint
|
||||
from langchain_community.llms import SagemakerEndpoint
|
||||
from langchain_community.llms.sagemaker_endpoint import LLMContentHandler
|
||||
```
|
||||
|
||||
## Chat models
|
||||
|
||||
### Bedrock Chat
|
||||
|
||||
See a [usage example](/docs/integrations/chat/bedrock).
|
||||
|
||||
```python
|
||||
from langchain_community.chat_models import BedrockChat
|
||||
```
|
||||
|
||||
## Embedding Models
|
||||
@@ -199,16 +188,16 @@ from langchain.vectorstores import DocumentDBVectorSearch
|
||||
> manuals, and websites. It supports multiple languages and can understand complex queries, synonyms, and
|
||||
> contextual meanings to provide highly relevant search results.
|
||||
|
||||
We need to install the `langchain-aws` library.
|
||||
We need to install the `boto3` library.
|
||||
|
||||
```bash
|
||||
pip install langchain-aws
|
||||
pip install boto3
|
||||
```
|
||||
|
||||
See a [usage example](/docs/integrations/retrievers/amazon_kendra_retriever).
|
||||
|
||||
```python
|
||||
from langchain_aws import AmazonKendraRetriever
|
||||
from langchain.retrievers import AmazonKendraRetriever
|
||||
```
|
||||
|
||||
### Amazon Bedrock (Knowledge Bases)
|
||||
@@ -217,16 +206,16 @@ from langchain_aws import AmazonKendraRetriever
|
||||
> `Amazon Web Services` (`AWS`) offering which lets you quickly build RAG applications by using your
|
||||
> private data to customize foundation model response.
|
||||
|
||||
We need to install the `langchain-aws` library.
|
||||
We need to install the `boto3` library.
|
||||
|
||||
```bash
|
||||
pip install langchain-aws
|
||||
pip install boto3
|
||||
```
|
||||
|
||||
See a [usage example](/docs/integrations/retrievers/bedrock).
|
||||
|
||||
```python
|
||||
from langchain_aws import AmazonKnowledgeBasesRetriever
|
||||
from langchain.retrievers import AmazonKnowledgeBasesRetriever
|
||||
```
|
||||
|
||||
## Tools
|
||||
|
||||
@@ -486,21 +486,6 @@ See [usage example](/docs/integrations/vectorstores/google_spanner).
|
||||
from langchain_google_spanner import SpannerVectorStore
|
||||
```
|
||||
|
||||
### Firestore (Native Mode)
|
||||
|
||||
> [Google Cloud Firestore](https://cloud.google.com/firestore/docs/) is a NoSQL document database built for automatic scaling, high performance, and ease of application development.
|
||||
Install the python package:
|
||||
|
||||
```bash
|
||||
pip install langchain-google-firestore
|
||||
```
|
||||
|
||||
See [usage example](/docs/integrations/vectorstores/google_firestore).
|
||||
|
||||
```python
|
||||
from langchain_google_firestore import FirestoreVectorstore
|
||||
```
|
||||
|
||||
### Cloud SQL for MySQL
|
||||
|
||||
> [Google Cloud SQL for MySQL](https://cloud.google.com/sql) is a fully-managed database service that helps you set up, maintain, manage, and administer your MySQL relational databases on Google Cloud.
|
||||
|
||||
@@ -13,7 +13,6 @@ These providers have standalone `langchain-{provider}` packages for improved ver
|
||||
|
||||
- [AI21](/docs/integrations/providers/ai21)
|
||||
- [Airbyte](/docs/integrations/providers/airbyte)
|
||||
- [Amazon Web Services](/docs/integrations/platforms/aws)
|
||||
- [Anthropic](/docs/integrations/platforms/anthropic)
|
||||
- [Astra DB](/docs/integrations/providers/astradb)
|
||||
- [Cohere](/docs/integrations/providers/cohere)
|
||||
@@ -31,12 +30,12 @@ These providers have standalone `langchain-{provider}` packages for improved ver
|
||||
- [Pinecone](/docs/integrations/providers/pinecone)
|
||||
- [Robocorp](/docs/integrations/providers/robocorp)
|
||||
- [Together AI](/docs/integrations/providers/together)
|
||||
- [Upstage](/docs/integrations/providers/upstage)
|
||||
- [Voyage AI](/docs/integrations/providers/voyageai)
|
||||
|
||||
|
||||
## Featured Community Providers
|
||||
|
||||
- [AWS](/docs/integrations/platforms/aws)
|
||||
- [Hugging Face](/docs/integrations/platforms/huggingface)
|
||||
- [Microsoft](/docs/integrations/platforms/microsoft)
|
||||
|
||||
|
||||
@@ -174,7 +174,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain_core.prompts import PromptTemplate"
|
||||
"from langchain.prompts import PromptTemplate"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -10,24 +10,6 @@
|
||||
> Alibaba's own e-commerce ecosystem.
|
||||
|
||||
|
||||
## LLMs
|
||||
|
||||
### Alibaba Cloud PAI EAS
|
||||
|
||||
See [installation instructions and a usage example](/docs/integrations/llms/alibabacloud_pai_eas_endpoint).
|
||||
|
||||
```python
|
||||
from langchain_community.llms.pai_eas_endpoint import PaiEasEndpoint
|
||||
```
|
||||
|
||||
### Tongyi Qwen
|
||||
|
||||
See [installation instructions and a usage example](/docs/integrations/llms/tongyi).
|
||||
|
||||
```python
|
||||
from langchain_community.llms import Tongyi
|
||||
```
|
||||
|
||||
## Chat Models
|
||||
|
||||
### Alibaba Cloud PAI EAS
|
||||
@@ -38,24 +20,6 @@ See [installation instructions and a usage example](/docs/integrations/chat/alib
|
||||
from langchain_community.chat_models import PaiEasChatEndpoint
|
||||
```
|
||||
|
||||
### Tongyi Qwen Chat
|
||||
|
||||
See [installation instructions and a usage example](/docs/integrations/chat/tongyi).
|
||||
|
||||
```python
|
||||
from langchain_community.chat_models.tongyi import ChatTongyi
|
||||
```
|
||||
|
||||
## Document Loaders
|
||||
|
||||
### Alibaba Cloud MaxCompute
|
||||
|
||||
See [installation instructions and a usage example](/docs/integrations/document_loaders/alibaba_cloud_maxcompute).
|
||||
|
||||
```python
|
||||
from langchain_community.document_loaders import MaxComputeLoader
|
||||
```
|
||||
|
||||
## Vector stores
|
||||
|
||||
### Alibaba Cloud OpenSearch
|
||||
@@ -74,18 +38,12 @@ See [installation instructions and a usage example](/docs/integrations/vectorsto
|
||||
from langchain_community.vectorstores import Tair
|
||||
```
|
||||
|
||||
### AnalyticDB
|
||||
## Document Loaders
|
||||
|
||||
See [installation instructions and a usage example](/docs/integrations/vectorstores/analyticdb).
|
||||
### Alibaba Cloud MaxCompute
|
||||
|
||||
See [installation instructions and a usage example](/docs/integrations/document_loaders/alibaba_cloud_maxcompute).
|
||||
|
||||
```python
|
||||
from langchain_community.vectorstores import AnalyticDB
|
||||
```
|
||||
|
||||
### Hologres
|
||||
|
||||
See [installation instructions and a usage example](/docs/integrations/vectorstores/hologres).
|
||||
|
||||
```python
|
||||
from langchain_community.vectorstores import Hologres
|
||||
from langchain_community.document_loaders import MaxComputeLoader
|
||||
```
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
## Installation and Setup
|
||||
|
||||
```bash
|
||||
pip install langchain-chroma
|
||||
pip install chromadb
|
||||
```
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ There exists a wrapper around Chroma vector databases, allowing you to use it as
|
||||
whether for semantic search or example selection.
|
||||
|
||||
```python
|
||||
from langchain_chroma import Chroma
|
||||
from langchain_community.vectorstores import Chroma
|
||||
```
|
||||
|
||||
For a more detailed walkthrough of the Chroma wrapper, see [this notebook](/docs/integrations/vectorstores/chroma)
|
||||
|
||||
@@ -51,7 +51,7 @@ Usage of the Cohere (legacy) [LLM model](/docs/integrations/llms/cohere)
|
||||
```python
|
||||
from langchain_community.tools.tavily_search import TavilySearchResults
|
||||
from langchain_cohere import ChatCohere, create_cohere_react_agent
|
||||
from langchain_core.prompts import ChatPromptTemplate
|
||||
from langchain.prompts import ChatPromptTemplate
|
||||
from langchain.agents import AgentExecutor
|
||||
|
||||
llm = ChatCohere()
|
||||
|
||||
@@ -154,7 +154,7 @@
|
||||
"source": [
|
||||
"from langchain.callbacks import CometCallbackHandler, StdOutCallbackHandler\n",
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_openai import OpenAI\n",
|
||||
"\n",
|
||||
"comet_callback = CometCallbackHandler(\n",
|
||||
@@ -251,7 +251,7 @@
|
||||
"source": [
|
||||
"from langchain.callbacks import CometCallbackHandler, StdOutCallbackHandler\n",
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_openai import OpenAI\n",
|
||||
"from rouge_score import rouge_scorer\n",
|
||||
"\n",
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
# Dataherald
|
||||
|
||||
>[Dataherald](https://www.dataherald.com) is a natural language-to-SQL.
|
||||
|
||||
This page covers how to use the `Dataherald API` within LangChain.
|
||||
|
||||
## Installation and Setup
|
||||
- Install requirements with
|
||||
```bash
|
||||
pip install dataherald
|
||||
```
|
||||
- Go to dataherald and sign up [here](https://www.dataherald.com)
|
||||
- Create an app and get your `API KEY`
|
||||
- Set your `API KEY` as an environment variable `DATAHERALD_API_KEY`
|
||||
|
||||
|
||||
## Wrappers
|
||||
|
||||
### Utility
|
||||
|
||||
There exists a DataheraldAPIWrapper utility which wraps this API. To import this utility:
|
||||
|
||||
```python
|
||||
from langchain_community.utilities.dataherald import DataheraldAPIWrapper
|
||||
```
|
||||
|
||||
For a more detailed walkthrough of this wrapper, see [this notebook](/docs/integrations/tools/dataherald).
|
||||
|
||||
### Tool
|
||||
|
||||
You can use the tool in an agent like this:
|
||||
```python
|
||||
from langchain_community.utilities.dataherald import DataheraldAPIWrapper
|
||||
from langchain_community.tools.dataherald.tool import DataheraldTextToSQL
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langchain import hub
|
||||
from langchain.agents import AgentExecutor, create_react_agent, load_tools
|
||||
|
||||
api_wrapper = DataheraldAPIWrapper(db_connection_id="<db_connection_id>")
|
||||
tool = DataheraldTextToSQL(api_wrapper=api_wrapper)
|
||||
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
|
||||
prompt = hub.pull("hwchase17/react")
|
||||
agent = create_react_agent(llm, tools, prompt)
|
||||
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
||||
agent_executor.invoke({"input":"Return the sql for this question: How many employees are in the company?"})
|
||||
```
|
||||
|
||||
Output
|
||||
```shell
|
||||
> Entering new AgentExecutor chain...
|
||||
I need to use a tool that can convert this question into SQL.
|
||||
Action: dataherald
|
||||
Action Input: How many employees are in the company?Answer: SELECT
|
||||
COUNT(*) FROM employeesI now know the final answer
|
||||
Final Answer: SELECT
|
||||
COUNT(*)
|
||||
FROM
|
||||
employees
|
||||
|
||||
> Finished chain.
|
||||
{'input': 'Return the sql for this question: How many employees are in the company?', 'output': "SELECT \n COUNT(*)\nFROM \n employees"}
|
||||
```
|
||||
|
||||
For more information on tools, see [this page](/docs/modules/tools/).
|
||||
@@ -29,7 +29,7 @@ from langchain.agents import AgentType, initialize_agent, load_tools
|
||||
from langchain.callbacks import FlyteCallbackHandler
|
||||
from langchain.chains import LLMChain
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
from langchain.prompts import PromptTemplate
|
||||
from langchain_core.messages import HumanMessage
|
||||
```
|
||||
|
||||
|
||||
@@ -37,13 +37,3 @@ See a [usage example](/docs/integrations/llms/ibm_watsonx).
|
||||
```python
|
||||
from langchain_ibm import WatsonxLLM
|
||||
```
|
||||
|
||||
## Embedding Models
|
||||
|
||||
### WatsonxEmbeddings
|
||||
|
||||
See a [usage example](/docs/integrations/text_embedding/ibm_watsonx).
|
||||
|
||||
```python
|
||||
from langchain_ibm import WatsonxEmbeddings
|
||||
```
|
||||
|
||||
@@ -31,7 +31,7 @@ export JAVELIN_API_KEY=...
|
||||
|
||||
from langchain.chains import LLMChain
|
||||
from langchain_community.llms import JavelinAIGateway
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
from langchain.prompts import PromptTemplate
|
||||
|
||||
route_completions = "eng_dept03"
|
||||
|
||||
|
||||
@@ -140,7 +140,7 @@ Please contact a Databricks representative to enroll in the preview.
|
||||
|
||||
```python
|
||||
from langchain.chains import LLMChain
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
from langchain.prompts import PromptTemplate
|
||||
from langchain_community.llms import MlflowAIGateway
|
||||
|
||||
gateway = MlflowAIGateway(
|
||||
|
||||
@@ -123,7 +123,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain_core.prompts import PromptTemplate"
|
||||
"from langchain.prompts import PromptTemplate"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1,174 +1,113 @@
|
||||
# Portkey
|
||||
|
||||
[Portkey](https://portkey.ai) is the Control Panel for AI apps. With it's popular AI Gateway and Observability Suite, hundreds of teams ship **reliable**, **cost-efficient**, and **fast** apps.
|
||||
>[Portkey](https://docs.portkey.ai/overview/introduction) is a platform designed to streamline the deployment
|
||||
> and management of Generative AI applications.
|
||||
> It provides comprehensive features for monitoring, managing models,
|
||||
> and improving the performance of your AI applications.
|
||||
|
||||
## LLMOps for Langchain
|
||||
|
||||
Portkey brings production readiness to Langchain. With Portkey, you can
|
||||
- [x] Connect to 150+ models through a unified API,
|
||||
- [x] View 42+ **metrics & logs** for all requests,
|
||||
- [x] Enable **semantic cache** to reduce latency & costs,
|
||||
- [x] Implement automatic **retries & fallbacks** for failed requests,
|
||||
- [x] Add **custom tags** to requests for better tracking and analysis and [more](https://portkey.ai/docs).
|
||||
- [x] view detailed **metrics & logs** for all requests,
|
||||
- [x] enable **semantic cache** to reduce latency & costs,
|
||||
- [x] implement automatic **retries & fallbacks** for failed requests,
|
||||
- [x] add **custom tags** to requests for better tracking and analysis and [more](https://docs.portkey.ai).
|
||||
|
||||
### Using Portkey with Langchain
|
||||
Using Portkey is as simple as just choosing which Portkey features you want, enabling them via `headers=Portkey.Config` and passing it in your LLM calls.
|
||||
|
||||
## Quickstart - Portkey & Langchain
|
||||
Since Portkey is fully compatible with the OpenAI signature, you can connect to the Portkey AI Gateway through the `ChatOpenAI` interface.
|
||||
To start, get your Portkey API key by [signing up here](https://app.portkey.ai/login). (Click the profile icon on the top left, then click on "Copy API Key")
|
||||
|
||||
- Set the `base_url` as `PORTKEY_GATEWAY_URL`
|
||||
- Add `default_headers` to consume the headers needed by Portkey using the `createHeaders` helper method.
|
||||
|
||||
To start, get your Portkey API key by [signing up here](https://app.portkey.ai/signup). (Click the profile icon on the bottom left, then click on "Copy API Key") or deploy the open source AI gateway in [your own environment](https://github.com/Portkey-AI/gateway/blob/main/docs/installation-deployments.md).
|
||||
|
||||
Next, install the Portkey SDK
|
||||
For OpenAI, a simple integration with logging feature would look like this:
|
||||
```python
|
||||
pip install -U portkey_ai
|
||||
```
|
||||
from langchain_openai import OpenAI
|
||||
from langchain_community.utilities import Portkey
|
||||
|
||||
We can now connect to the Portkey AI Gateway by updating the `ChatOpenAI` model in Langchain
|
||||
```python
|
||||
from langchain_openai import ChatOpenAI
|
||||
from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL
|
||||
|
||||
PORTKEY_API_KEY = "..." # Not needed when hosting your own gateway
|
||||
PROVIDER_API_KEY = "..." # Add the API key of the AI provider being used
|
||||
|
||||
portkey_headers = createHeaders(api_key=PORTKEY_API_KEY,provider="openai")
|
||||
|
||||
llm = ChatOpenAI(api_key=PROVIDER_API_KEY, base_url=PORTKEY_GATEWAY_URL, default_headers=portkey_headers)
|
||||
|
||||
llm.invoke("What is the meaning of life, universe and everything?")
|
||||
```
|
||||
|
||||
The request is routed through your Portkey AI Gateway to the specified `provider`. Portkey will also start logging all the requests in your account that makes debugging extremely simple.
|
||||
|
||||

|
||||
|
||||
## Using 150+ models through the AI Gateway
|
||||
The power of the AI gateway comes when you're able to use the above code snippet to connect with 150+ models across 20+ providers supported through the AI gateway.
|
||||
|
||||
Let's modify the code above to make a call to Anthropic's `claude-3-opus-20240229` model.
|
||||
|
||||
Portkey supports **[Virtual Keys](https://docs.portkey.ai/docs/product/ai-gateway-streamline-llm-integrations/virtual-keys)** which are an easy way to store and manage API keys in a secure vault. Lets try using a Virtual Key to make LLM calls. You can navigate to the Virtual Keys tab in Portkey and create a new key for Anthropic.
|
||||
|
||||
The `virtual_key` parameter sets the authentication and provider for the AI provider being used. In our case we're using the Anthropic Virtual key.
|
||||
|
||||
> Notice that the `api_key` can be left blank as that authentication won't be used.
|
||||
|
||||
```python
|
||||
from langchain_openai import ChatOpenAI
|
||||
from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL
|
||||
|
||||
PORTKEY_API_KEY = "..."
|
||||
VIRTUAL_KEY = "..." # Anthropic's virtual key we copied above
|
||||
|
||||
portkey_headers = createHeaders(api_key=PORTKEY_API_KEY,virtual_key=VIRTUAL_KEY)
|
||||
|
||||
llm = ChatOpenAI(api_key="X", base_url=PORTKEY_GATEWAY_URL, default_headers=portkey_headers, model="claude-3-opus-20240229")
|
||||
|
||||
llm.invoke("What is the meaning of life, universe and everything?")
|
||||
```
|
||||
|
||||
The Portkey AI gateway will authenticate the API request to Anthropic and get the response back in the OpenAI format for you to consume.
|
||||
|
||||
The AI gateway extends Langchain's `ChatOpenAI` class making it a single interface to call any provider and any model.
|
||||
|
||||
## Advanced Routing - Load Balancing, Fallbacks, Retries
|
||||
The Portkey AI Gateway brings capabilities like load-balancing, fallbacks, experimentation and canary testing to Langchain through a configuration-first approach.
|
||||
|
||||
Let's take an **example** where we might want to split traffic between `gpt-4` and `claude-opus` 50:50 to test the two large models. The gateway configuration for this would look like the following:
|
||||
|
||||
```python
|
||||
config = {
|
||||
"strategy": {
|
||||
"mode": "loadbalance"
|
||||
},
|
||||
"targets": [{
|
||||
"virtual_key": "openai-25654", # OpenAI's virtual key
|
||||
"override_params": {"model": "gpt4"},
|
||||
"weight": 0.5
|
||||
}, {
|
||||
"virtual_key": "anthropic-25654", # Anthropic's virtual key
|
||||
"override_params": {"model": "claude-3-opus-20240229"},
|
||||
"weight": 0.5
|
||||
}]
|
||||
}
|
||||
```
|
||||
|
||||
We can then use this config in our requests being made from langchain.
|
||||
|
||||
```python
|
||||
portkey_headers = createHeaders(
|
||||
api_key=PORTKEY_API_KEY,
|
||||
config=config
|
||||
# Add the Portkey API Key from your account
|
||||
headers = Portkey.Config(
|
||||
api_key = "<PORTKEY_API_KEY>"
|
||||
)
|
||||
|
||||
llm = ChatOpenAI(api_key="X", base_url=PORTKEY_GATEWAY_URL, default_headers=portkey_headers)
|
||||
|
||||
llm.invoke("What is the meaning of life, universe and everything?")
|
||||
llm = OpenAI(temperature=0.9, headers=headers)
|
||||
llm.predict("What would be a good company name for a company that makes colorful socks?")
|
||||
```
|
||||
Your logs will be captured on your [Portkey dashboard](https://app.portkey.ai).
|
||||
|
||||
When the LLM is invoked, Portkey will distribute the requests to `gpt-4` and `claude-3-opus-20240229` in the ratio of the defined weights.
|
||||
A common Portkey X Langchain use case is to **trace a chain or an agent** and view all the LLM calls originating from that request.
|
||||
|
||||
You can find more config examples [here](https://docs.portkey.ai/docs/api-reference/config-object#examples).
|
||||
|
||||
## **Tracing Chains & Agents**
|
||||
|
||||
Portkey's Langchain integration gives you full visibility into the running of an agent. Let's take an example of a [popular agentic workflow](https://python.langchain.com/docs/use_cases/tool_use/quickstart/#agents).
|
||||
|
||||
We only need to modify the `ChatOpenAI` class to use the AI Gateway as above.
|
||||
### **Tracing Chains & Agents**
|
||||
|
||||
```python
|
||||
from langchain import hub
|
||||
from langchain.agents import AgentExecutor, create_openai_tools_agent
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langchain_core.tools import tool
|
||||
from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders
|
||||
|
||||
prompt = hub.pull("hwchase17/openai-tools-agent")
|
||||
from langchain.agents import AgentType, initialize_agent, load_tools
|
||||
from langchain_openai import OpenAI
|
||||
from langchain_community.utilities import Portkey
|
||||
|
||||
portkey_headers = createHeaders(
|
||||
api_key=PORTKEY_API_KEY,
|
||||
virtual_key=OPENAI_VIRTUAL_KEY,
|
||||
trace_id="uuid-uuid-uuid-uuid"
|
||||
# Add the Portkey API Key from your account
|
||||
headers = Portkey.Config(
|
||||
api_key = "<PORTKEY_API_KEY>",
|
||||
trace_id = "fef659"
|
||||
)
|
||||
|
||||
@tool
|
||||
def multiply(first_int: int, second_int: int) -> int:
|
||||
"""Multiply two integers together."""
|
||||
return first_int * second_int
|
||||
llm = OpenAI(temperature=0, headers=headers)
|
||||
tools = load_tools(["serpapi", "llm-math"], llm=llm)
|
||||
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
|
||||
|
||||
|
||||
@tool
|
||||
def exponentiate(base: int, exponent: int) -> int:
|
||||
"Exponentiate the base to the exponent power."
|
||||
return base**exponent
|
||||
|
||||
|
||||
tools = [multiply, exponentiate]
|
||||
|
||||
model = ChatOpenAI(api_key="X", base_url=PORTKEY_GATEWAY_URL, default_headers=portkey_headers, temperature=0)
|
||||
|
||||
# Construct the OpenAI Tools agent
|
||||
agent = create_openai_tools_agent(model, tools, prompt)
|
||||
|
||||
# Create an agent executor by passing in the agent and tools
|
||||
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
||||
|
||||
agent_executor.invoke({
|
||||
"input": "Take 3 to the fifth power and multiply that by thirty six, then square the result"
|
||||
})
|
||||
# Let's test it out!
|
||||
agent.run("What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?")
|
||||
```
|
||||
|
||||
**You can see the requests' logs along with the trace id on Portkey dashboard:**
|
||||

|
||||
|
||||
<img src="/img/portkey-dashboard.gif" height="250"/>
|
||||
<img src="/img/portkey-tracing.png" height="250"/>
|
||||
|
||||
## Advanced Features
|
||||
|
||||
1. **Logging:** Log all your LLM requests automatically by sending them through Portkey. Each request log contains `timestamp`, `model name`, `total cost`, `request time`, `request json`, `response json`, and additional Portkey features.
|
||||
2. **Tracing:** Trace id can be passed along with each request and is visibe on the logs on Portkey dashboard. You can also set a **distinct trace id** for each request. You can [append user feedback](https://docs.portkey.ai/key-features/feedback-api) to a trace id as well.
|
||||
3. **Caching:** Respond to previously served customers queries from cache instead of sending them again to OpenAI. Match exact strings OR semantically similar strings. Cache can save costs and reduce latencies by 20x.
|
||||
4. **Retries:** Automatically reprocess any unsuccessful API requests **`upto 5`** times. Uses an **`exponential backoff`** strategy, which spaces out retry attempts to prevent network overload.
|
||||
5. **Tagging:** Track and audit each user interaction in high detail with predefined tags.
|
||||
|
||||
| Feature | Config Key | Value (Type) | Required/Optional |
|
||||
| -- | -- | -- | -- |
|
||||
| API Key | `api_key` | API Key (`string`) | ✅ Required |
|
||||
| [Tracing Requests](https://docs.portkey.ai/key-features/request-tracing) | `trace_id` | Custom `string` | ❔ Optional |
|
||||
| [Automatic Retries](https://docs.portkey.ai/key-features/automatic-retries) | `retry_count` | `integer` [1,2,3,4,5] | ❔ Optional |
|
||||
| [Enabling Cache](https://docs.portkey.ai/key-features/request-caching) | `cache` | `simple` OR `semantic` | ❔ Optional |
|
||||
| Cache Force Refresh | `cache_force_refresh` | `True` | ❔ Optional |
|
||||
| Set Cache Expiry | `cache_age` | `integer` (in seconds) | ❔ Optional |
|
||||
| [Add User](https://docs.portkey.ai/key-features/custom-metadata) | `user` | `string` | ❔ Optional |
|
||||
| [Add Organisation](https://docs.portkey.ai/key-features/custom-metadata) | `organisation` | `string` | ❔ Optional |
|
||||
| [Add Environment](https://docs.portkey.ai/key-features/custom-metadata) | `environment` | `string` | ❔ Optional |
|
||||
| [Add Prompt (version/id/string)](https://docs.portkey.ai/key-features/custom-metadata) | `prompt` | `string` | ❔ Optional |
|
||||
|
||||
|
||||
Additional Docs are available here:
|
||||
- Observability - https://portkey.ai/docs/product/observability-modern-monitoring-for-llms
|
||||
- AI Gateway - https://portkey.ai/docs/product/ai-gateway-streamline-llm-integrations
|
||||
- Prompt Library - https://portkey.ai/docs/product/prompt-library
|
||||
## **Enabling all Portkey Features:**
|
||||
|
||||
You can check out our popular Open Source AI Gateway here - https://github.com/portkey-ai/gateway
|
||||
```py
|
||||
headers = Portkey.Config(
|
||||
|
||||
# Mandatory
|
||||
api_key="<PORTKEY_API_KEY>",
|
||||
|
||||
# Cache Options
|
||||
cache="semantic",
|
||||
cache_force_refresh="True",
|
||||
cache_age=1729,
|
||||
|
||||
For detailed information on each feature and how to use it, [please refer to the Portkey docs](https://portkey.ai/docs). If you have any questions or need further assistance, [reach out to us on Twitter.](https://twitter.com/portkeyai) or our [support email](mailto:hello@portkey.ai).
|
||||
# Advanced
|
||||
retry_count=5,
|
||||
trace_id="langchain_agent",
|
||||
|
||||
# Metadata
|
||||
environment="production",
|
||||
user="john",
|
||||
organisation="acme",
|
||||
prompt="Frost"
|
||||
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
For detailed information on each feature and how to use it, [please refer to the Portkey docs](https://docs.portkey.ai). If you have any questions or need further assistance, [reach out to us on Twitter.](https://twitter.com/portkeyai).
|
||||
@@ -6,7 +6,7 @@
|
||||
"source": [
|
||||
"# Log, Trace, and Monitor\n",
|
||||
"\n",
|
||||
"When building apps or agents using Langchain, you end up making multiple API calls to fulfill a single user request. However, these requests are not chained when you want to analyse them. With [**Portkey**](/docs/integrations/providers/portkey/), all the embeddings, completions, and other requests from a single user request will get logged and traced to a common ID, enabling you to gain full visibility of user interactions.\n",
|
||||
"When building apps or agents using Langchain, you end up making multiple API calls to fulfill a single user request. However, these requests are not chained when you want to analyse them. With [**Portkey**](/docs/integrations/providers/portkey), all the embeddings, completion, and other requests from a single user request will get logged and traced to a common ID, enabling you to gain full visibility of user interactions.\n",
|
||||
"\n",
|
||||
"This notebook serves as a step-by-step guide on how to log, trace, and monitor Langchain LLM calls using `Portkey` in your Langchain app."
|
||||
]
|
||||
@@ -20,15 +20,15 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"from langchain.agents import AgentExecutor, create_openai_tools_agent\n",
|
||||
"from langchain_openai import ChatOpenAI\n",
|
||||
"from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders"
|
||||
"from langchain.agents import AgentType, initialize_agent, load_tools\n",
|
||||
"from langchain_community.utilities import Portkey\n",
|
||||
"from langchain_openai import OpenAI"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -40,11 +40,11 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"os.environ[\"OPENAI_API_KEY\"] = \"...\""
|
||||
"os.environ[\"OPENAI_API_KEY\"] = \"<OPENAI_API_KEY>\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -52,18 +52,18 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Get Portkey API Key\n",
|
||||
"1. Sign up for [Portkey here](https://app.portkey.ai/signup)\n",
|
||||
"2. On your [dashboard](https://app.portkey.ai/), click on the profile icon on the bottom left, then click on \"Copy API Key\"\n",
|
||||
"1. Sign up for [Portkey here](https://app.portkey.ai/login)\n",
|
||||
"2. On your [dashboard](https://app.portkey.ai/), click on the profile icon on the top left, then click on \"Copy API Key\"\n",
|
||||
"3. Paste it below"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"PORTKEY_API_KEY = \"...\" # Paste your Portkey API Key here"
|
||||
"PORTKEY_API_KEY = \"<PORTKEY_API_KEY>\" # Paste your Portkey API Key here"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -77,11 +77,11 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"TRACE_ID = \"uuid-trace-id\" # Set trace id here"
|
||||
"TRACE_ID = \"portkey_langchain_demo\" # Set trace id here"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -93,49 +93,16 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"portkey_headers = createHeaders(\n",
|
||||
" api_key=PORTKEY_API_KEY, provider=\"openai\", trace_id=TRACE_ID\n",
|
||||
"headers = Portkey.Config(\n",
|
||||
" api_key=PORTKEY_API_KEY,\n",
|
||||
" trace_id=TRACE_ID,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Define the prompts and the tools to use"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain import hub\n",
|
||||
"from langchain_core.tools import tool\n",
|
||||
"\n",
|
||||
"prompt = hub.pull(\"hwchase17/openai-tools-agent\")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@tool\n",
|
||||
"def multiply(first_int: int, second_int: int) -> int:\n",
|
||||
" \"\"\"Multiply two integers together.\"\"\"\n",
|
||||
" return first_int * second_int\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@tool\n",
|
||||
"def exponentiate(base: int, exponent: int) -> int:\n",
|
||||
" \"Exponentiate the base to the exponent power.\"\n",
|
||||
" return base**exponent\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"tools = [multiply, exponentiate]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
@@ -145,60 +112,19 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||||
"\u001b[32;1m\u001b[1;3m\n",
|
||||
"Invoking: `exponentiate` with `{'base': 3, 'exponent': 5}`\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[0m\u001b[33;1m\u001b[1;3m243\u001b[0m\u001b[32;1m\u001b[1;3m\n",
|
||||
"Invoking: `multiply` with `{'first_int': 243, 'second_int': 36}`\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[0m\u001b[36;1m\u001b[1;3m8748\u001b[0m\u001b[32;1m\u001b[1;3m\n",
|
||||
"Invoking: `exponentiate` with `{'base': 8748, 'exponent': 2}`\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\u001b[0m\u001b[33;1m\u001b[1;3m76527504\u001b[0m\u001b[32;1m\u001b[1;3mThe result of taking 3 to the fifth power, multiplying it by 36, and then squaring the result is 76,527,504.\u001b[0m\n",
|
||||
"\n",
|
||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'input': 'Take 3 to the fifth power and multiply that by thirty six, then square the result',\n",
|
||||
" 'output': 'The result of taking 3 to the fifth power, multiplying it by 36, and then squaring the result is 76,527,504.'}"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = ChatOpenAI(\n",
|
||||
" base_url=PORTKEY_GATEWAY_URL, default_headers=portkey_headers, temperature=0\n",
|
||||
"llm = OpenAI(temperature=0, headers=headers)\n",
|
||||
"tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)\n",
|
||||
"agent = initialize_agent(\n",
|
||||
" tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Construct the OpenAI Tools agent\n",
|
||||
"agent = create_openai_tools_agent(model, tools, prompt)\n",
|
||||
"\n",
|
||||
"# Create an agent executor by passing in the agent and tools\n",
|
||||
"agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n",
|
||||
"\n",
|
||||
"agent_executor.invoke(\n",
|
||||
" {\n",
|
||||
" \"input\": \"Take 3 to the fifth power and multiply that by thirty six, then square the result\"\n",
|
||||
" }\n",
|
||||
"# Let's test it out!\n",
|
||||
"agent.run(\n",
|
||||
" \"What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -212,13 +138,10 @@
|
||||
"- Sending your request through Portkey ensures that all of the requests are logged by default\n",
|
||||
"- Each request log contains `timestamp`, `model name`, `total cost`, `request time`, `request json`, `response json`, and additional Portkey features\n",
|
||||
"\n",
|
||||
"**[Tracing](https://portkey.ai/docs/product/observability-modern-monitoring-for-llms/traces)**\n",
|
||||
"- Trace id is passed along with each request and is visible on the logs on Portkey dashboard\n",
|
||||
"**Tracing**\n",
|
||||
"- Trace id is passed along with each request and is visibe on the logs on Portkey dashboard\n",
|
||||
"- You can also set a **distinct trace id** for each request if you want\n",
|
||||
"- You can append user feedback to a trace id as well. [More info on this here](https://portkey.ai/docs/product/observability-modern-monitoring-for-llms/feedback)\n",
|
||||
"\n",
|
||||
"For the above request, you will be able to view the entire log trace like this\n",
|
||||
""
|
||||
"- You can append user feedback to a trace id as well. [More info on this here](https://docs.portkey.ai/key-features/feedback-api)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -231,15 +154,62 @@
|
||||
"\n",
|
||||
"**Caching**\n",
|
||||
"\n",
|
||||
"Respond to previously served customers queries from cache instead of sending them again to OpenAI. Match exact strings OR semantically similar strings. Cache can save costs and reduce latencies by 20x. [Docs](https://portkey.ai/docs/product/ai-gateway-streamline-llm-integrations/cache-simple-and-semantic)\n",
|
||||
"Respond to previously served customers queries from cache instead of sending them again to OpenAI. Match exact strings OR semantically similar strings. Cache can save costs and reduce latencies by 20x.\n",
|
||||
"\n",
|
||||
"**Retries**\n",
|
||||
"\n",
|
||||
"Automatically reprocess any unsuccessful API requests **`upto 5`** times. Uses an **`exponential backoff`** strategy, which spaces out retry attempts to prevent network overload.[Docs](https://portkey.ai/docs/product/ai-gateway-streamline-llm-integrations)\n",
|
||||
"Automatically reprocess any unsuccessful API requests **`upto 5`** times. Uses an **`exponential backoff`** strategy, which spaces out retry attempts to prevent network overload.\n",
|
||||
"\n",
|
||||
"| Feature | Config Key | Value (Type) |\n",
|
||||
"| -- | -- | -- |\n",
|
||||
"| [🔁 Automatic Retries](https://docs.portkey.ai/key-features/automatic-retries) | `retry_count` | `integer` [1,2,3,4,5] |\n",
|
||||
"| [🧠 Enabling Cache](https://docs.portkey.ai/key-features/request-caching) | `cache` | `simple` OR `semantic` |\n",
|
||||
"\n",
|
||||
"**Tagging**\n",
|
||||
"\n",
|
||||
"Track and audit each user interaction in high detail with predefined tags. [Docs](https://portkey.ai/docs/product/observability-modern-monitoring-for-llms/metadata)"
|
||||
"Track and audit ach user interaction in high detail with predefined tags.\n",
|
||||
"\n",
|
||||
"| Tag | Config Key | Value (Type) |\n",
|
||||
"| -- | -- | -- |\n",
|
||||
"| User Tag | `user` | `string` |\n",
|
||||
"| Organisation Tag | `organisation` | `string` |\n",
|
||||
"| Environment Tag | `environment` | `string` |\n",
|
||||
"| Prompt Tag (version/id/string) | `prompt` | `string` |"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code Example With All Features"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"headers = Portkey.Config(\n",
|
||||
" # Mandatory\n",
|
||||
" api_key=\"<PORTKEY_API_KEY>\",\n",
|
||||
" # Cache Options\n",
|
||||
" cache=\"semantic\",\n",
|
||||
" cache_force_refresh=\"True\",\n",
|
||||
" cache_age=1729,\n",
|
||||
" # Advanced\n",
|
||||
" retry_count=5,\n",
|
||||
" trace_id=\"langchain_agent\",\n",
|
||||
" # Metadata\n",
|
||||
" environment=\"production\",\n",
|
||||
" user=\"john\",\n",
|
||||
" organisation=\"acme\",\n",
|
||||
" prompt=\"Frost\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"llm = OpenAI(temperature=0.9, headers=headers)\n",
|
||||
"\n",
|
||||
"print(llm(\"Two roads diverged in the yellow woods\"))"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -259,7 +229,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.1"
|
||||
"version": "3.10.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -37,7 +37,7 @@ import os
|
||||
|
||||
import predictionguard as pg
|
||||
from langchain_community.llms import PredictionGuard
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
from langchain.prompts import PromptTemplate
|
||||
from langchain.chains import LLMChain
|
||||
|
||||
# Your Prediction Guard API key. Get one at predictionguard.com
|
||||
@@ -77,7 +77,7 @@ Basic LLM Chaining with the Prediction Guard wrapper:
|
||||
```python
|
||||
import os
|
||||
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
from langchain.prompts import PromptTemplate
|
||||
from langchain.chains import LLMChain
|
||||
from langchain_community.llms import PredictionGuard
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_openai import OpenAI"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -104,7 +104,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import LLMChain\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_openai import OpenAI\n",
|
||||
"\n",
|
||||
"# Set up the LangChain SDK with the environment variable\n",
|
||||
|
||||
@@ -20,7 +20,7 @@ As of June 2023, the API supports Vicuna-13B by default. We are going to support
|
||||
For example
|
||||
```python
|
||||
from langchain_openai import OpenAI
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
from langchain.prompts import PromptTemplate
|
||||
from langchain.chains import LLMChain
|
||||
|
||||
import os
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
# Snowflake
|
||||
|
||||
> [Snowflake](https://www.snowflake.com/) is a cloud-based data-warehousing platform
|
||||
> that allows you to store and query large amounts of data.
|
||||
|
||||
This page covers how to use the `Snowflake` ecosystem within `LangChain`.
|
||||
|
||||
## Embedding models
|
||||
|
||||
Snowflake offers their open weight `arctic` line of embedding models for free
|
||||
on [Hugging Face](https://huggingface.co/Snowflake/snowflake-arctic-embed-l).
|
||||
You can use these models via the
|
||||
[HuggingFaceEmbeddings](/docs/integrations/text_embedding/huggingfacehub) connector:
|
||||
|
||||
```shell
|
||||
pip install langchain-community sentence-transformers
|
||||
```
|
||||
|
||||
```python
|
||||
from langchain_community.text_embeddings import HuggingFaceEmbeddings
|
||||
|
||||
model = HuggingFaceEmbeddings(model_name="snowflake/arctic-embed-l")
|
||||
```
|
||||
|
||||
## Document loader
|
||||
|
||||
You can use the [`SnowflakeLoader`](/docs/integrations/document_loaders/snowflake)
|
||||
to load data from Snowflake:
|
||||
|
||||
```python
|
||||
from langchain_community.document_loaders import SnowflakeLoader
|
||||
```
|
||||
@@ -1,149 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Upstage\n",
|
||||
"\n",
|
||||
"[Upstage](https://upstage.ai) is a leading artificial intelligence (AI) company specializing in delivering above-human-grade performance LLM components. \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Solar LLM\n",
|
||||
"\n",
|
||||
"**Solar Mini Chat** is a fast yet powerful advanced large language model focusing on English and Korean. It has been specifically fine-tuned for multi-turn chat purposes, showing enhanced performance across a wide range of natural language processing tasks, like multi-turn conversation or tasks that require an understanding of long contexts, such as RAG (Retrieval-Augmented Generation), compared to other models of a similar size. This fine-tuning equips it with the ability to handle longer conversations more effectively, making it particularly adept for interactive applications."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Installation and Setup\n",
|
||||
"\n",
|
||||
"Install `langchain-upstage` package:\n",
|
||||
"\n",
|
||||
"```bash\n",
|
||||
"pip install -qU langchain-core langchain-upstage\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Get an [access token](https://console.upstage.ai) and set it as an environment variable (`UPSTAGE_API_KEY`)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Upstage LangChain integrations\n",
|
||||
"\n",
|
||||
"| API | Description | Import | Example usage |\n",
|
||||
"| --- | --- | --- | --- |\n",
|
||||
"| Chat | Build assistants using Solar Mini Chat | `from langchain_upstage import ChatUpstage` | [Go](../../chat/upstage) |\n",
|
||||
"| Text Embedding | Embed strings to vectors | `from langchain_upstage import UpstageEmbeddings` | [Go](../../text_embedding/upstage) |\n",
|
||||
"\n",
|
||||
"See [documentations](https://developers.upstage.ai/) for more details about the features."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Quick Examples\n",
|
||||
"\n",
|
||||
"### Environment Setup"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"os.environ[\"UPSTAGE_API_KEY\"] = \"YOUR_API_KEY\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"\n",
|
||||
"### Chat\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_upstage import ChatUpstage\n",
|
||||
"\n",
|
||||
"chat = ChatUpstage()\n",
|
||||
"response = chat.invoke(\"Hello, how are you?\")\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"\n",
|
||||
"\n",
|
||||
"### Text embedding\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_upstage import UpstageEmbeddings\n",
|
||||
"\n",
|
||||
"embeddings = UpstageEmbeddings()\n",
|
||||
"doc_result = embeddings.embed_documents(\n",
|
||||
" [\"Sam is a teacher.\", \"This is another document\"]\n",
|
||||
")\n",
|
||||
"print(doc_result)\n",
|
||||
"\n",
|
||||
"query_result = embeddings.embed_query(\"What does Sam do?\")\n",
|
||||
"print(query_result)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"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.10.11"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
# UpTrain
|
||||
|
||||
>[UpTrain](https://uptrain.ai/) is an open-source unified platform to evaluate and
|
||||
>improve Generative AI applications. It provides grades for 20+ preconfigured evaluations
|
||||
>(covering language, code, embedding use cases), performs root cause analysis on failure
|
||||
>cases and gives insights on how to resolve them.
|
||||
|
||||
## Installation and Setup
|
||||
|
||||
```bash
|
||||
pip install uptrain
|
||||
```
|
||||
|
||||
## Callbacks
|
||||
|
||||
```python
|
||||
from langchain_community.callbacks.uptrain_callback import UpTrainCallbackHandler
|
||||
```
|
||||
|
||||
See an [example](/docs/integrations/callbacks/uptrain).
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user