langchain/cookbook/selecting_llms_based_on_context_length.ipynb

176 lines
3.9 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"id": "e93283d1",
"metadata": {},
"source": [
"# Selecting LLMs based on Context Length\n",
"\n",
"Different LLMs have different context lengths. As a very immediate an practical example, OpenAI has two versions of GPT-3.5-Turbo: one with 4k context, another with 16k context. This notebook shows how to route between them based on input."
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "cc453450",
"metadata": {},
"outputs": [],
"source": [
2023-11-14 22:17:44 +00:00
"from langchain.prompts import PromptTemplate\n",
docs[patch], templates[patch]: Import from core (#14575) Update imports to use core for the low-hanging fruit changes. Ran following ```bash git grep -l 'langchain.schema.runnable' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.runnable/langchain_core.runnables/g' git grep -l 'langchain.schema.output_parser' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.output_parser/langchain_core.output_parsers/g' git grep -l 'langchain.schema.messages' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.messages/langchain_core.messages/g' git grep -l 'langchain.schema.chat_histry' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.chat_history/langchain_core.chat_history/g' git grep -l 'langchain.schema.prompt_template' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.prompt_template/langchain_core.prompts/g' git grep -l 'from langchain.pydantic_v1' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.pydantic_v1/from langchain_core.pydantic_v1/g' git grep -l 'from langchain.tools.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.tools\.base/from langchain_core.tools/g' git grep -l 'from langchain.chat_models.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.chat_models.base/from langchain_core.language_models.chat_models/g' git grep -l 'from langchain.llms.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.llms\.base\ /from langchain_core.language_models.llms\ /g' git grep -l 'from langchain.embeddings.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.embeddings\.base/from langchain_core.embeddings/g' git grep -l 'from langchain.vectorstores.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.vectorstores\.base/from langchain_core.vectorstores/g' git grep -l 'from langchain.agents.tools' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.agents\.tools/from langchain_core.tools/g' git grep -l 'from langchain.schema.output' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.output\ /from langchain_core.outputs\ /g' git grep -l 'from langchain.schema.embeddings' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.embeddings/from langchain_core.embeddings/g' git grep -l 'from langchain.schema.document' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.document/from langchain_core.documents/g' git grep -l 'from langchain.schema.agent' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.agent/from langchain_core.agents/g' git grep -l 'from langchain.schema.prompt ' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.prompt\ /from langchain_core.prompt_values /g' git grep -l 'from langchain.schema.language_model' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.language_model/from langchain_core.language_models/g' ```
2023-12-12 00:49:10 +00:00
"from langchain_core.output_parsers import StrOutputParser\n",
"from langchain_core.prompt_values import PromptValue\n",
"from langchain_openai import ChatOpenAI"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1cec6a10",
"metadata": {},
"outputs": [],
"source": [
"short_context_model = ChatOpenAI(model=\"gpt-3.5-turbo\")\n",
"long_context_model = ChatOpenAI(model=\"gpt-3.5-turbo-16k\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "772da153",
"metadata": {},
"outputs": [],
"source": [
"def get_context_length(prompt: PromptValue):\n",
" messages = prompt.to_messages()\n",
" tokens = short_context_model.get_num_tokens_from_messages(messages)\n",
" return tokens"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "db771e20",
"metadata": {},
"outputs": [],
"source": [
"prompt = PromptTemplate.from_template(\"Summarize this passage: {context}\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "af057e2f",
"metadata": {},
"outputs": [],
"source": [
"def choose_model(prompt: PromptValue):\n",
" context_len = get_context_length(prompt)\n",
" if context_len < 30:\n",
" print(\"short model\")\n",
" return short_context_model\n",
" else:\n",
" print(\"long model\")\n",
" return long_context_model"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "84f3e07d",
"metadata": {},
"outputs": [],
"source": [
"chain = prompt | choose_model | StrOutputParser()"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "d8b14f8f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"short model\n"
]
},
{
"data": {
"text/plain": [
"'The passage mentions that a frog visited a pond.'"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chain.invoke({\"context\": \"a frog went to a pond\"})"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "70ebd3dd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"long model\n"
]
},
{
"data": {
"text/plain": [
"'The passage describes a frog that moved from one pond to another and perched on a log.'"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-10-29 22:50:09 +00:00
"chain.invoke(\n",
" {\"context\": \"a frog went to a pond and sat on a log and went to a different pond\"}\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a7e29fef",
"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.10.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}