diff --git a/docs/docs/integrations/chat/writer.ipynb b/docs/docs/integrations/chat/writer.ipynb index d7a47b9c4767a..9efa41f6f4799 100644 --- a/docs/docs/integrations/chat/writer.ipynb +++ b/docs/docs/integrations/chat/writer.ipynb @@ -1,14 +1,14 @@ { "cells": [ { - "metadata": {}, "cell_type": "raw", + "id": "85e07aae70a15572", + "metadata": {}, "source": [ "---\n", "sidebar_label: Writer\n", "---" - ], - "id": "85e07aae70a15572" + ] }, { "cell_type": "markdown", @@ -17,7 +17,7 @@ "source": [ "# ChatWriter\n", "\n", - "This notebook provides a quick overview for getting started with Writer [chat models](/docs/concepts/#chat-models).\n", + "This notebook provides a quick overview for getting started with Writer [chat models](/docs/concepts/chat_models).\n", "\n", "Writer has several chat models. You can find information about their latest models and their costs, context windows, and supported input types in the [Writer docs](https://dev.writer.com/home/models).\n", "\n", @@ -25,8 +25,9 @@ ] }, { - "metadata": {}, "cell_type": "markdown", + "id": "617a6e98205ab7c8", + "metadata": {}, "source": [ "## Overview\n", "\n", @@ -47,11 +48,11 @@ "### Credentials\n", "\n", "Head to [Writer AI Studio](https://app.writer.com/aistudio/signup?utm_campaign=devrel) to sign up to OpenAI and generate an API key. Once you've done this set the WRITER_API_KEY environment variable:" - ], - "id": "617a6e98205ab7c8" + ] }, { "cell_type": "code", + "execution_count": 1, "id": "e817fe2e-4f1d-4533-b19e-2400b1cf6ce8", "metadata": { "ExecuteTime": { @@ -59,15 +60,14 @@ "start_time": "2024-11-14T09:27:59.652281Z" } }, + "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "if not os.environ.get(\"WRITER_API_KEY\"):\n", " os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key:\")" - ], - "outputs": [], - "execution_count": 1 + ] }, { "cell_type": "markdown", @@ -81,6 +81,7 @@ }, { "cell_type": "code", + "execution_count": 2, "id": "2113471c-75d7-45df-b784-d78da4ef7aba", "metadata": { "ExecuteTime": { @@ -88,20 +89,21 @@ "start_time": "2024-11-14T09:46:26.826112Z" } }, - "source": "%pip install -qU langchain-community writer-sdk", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\r\n", - "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m A new release of pip is available: \u001B[0m\u001B[31;49m24.2\u001B[0m\u001B[39;49m -> \u001B[0m\u001B[32;49m24.3.1\u001B[0m\r\n", - "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m To update, run: \u001B[0m\u001B[32;49mpip install --upgrade pip\u001B[0m\r\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\r\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\r\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], - "execution_count": 2 + "source": [ + "%pip install -qU langchain-community writer-sdk" + ] }, { "cell_type": "markdown", @@ -115,14 +117,16 @@ }, { "cell_type": "code", + "execution_count": 3, "id": "522686de", "metadata": { - "tags": [], "ExecuteTime": { "end_time": "2024-11-14T09:46:33.504711Z", "start_time": "2024-11-14T09:46:32.574505Z" - } + }, + "tags": [] }, + "outputs": [], "source": [ "from langchain_community.chat_models.writer import ChatWriter\n", "\n", @@ -132,9 +136,7 @@ " max_tokens=1000,\n", " # other params...\n", ")" - ], - "outputs": [], - "execution_count": 3 + ] }, { "cell_type": "markdown", @@ -146,14 +148,16 @@ }, { "cell_type": "code", + "execution_count": 4, "id": "ce16ad78-8e6f-48cd-954e-98be75eb5836", "metadata": { - "tags": [], "ExecuteTime": { "end_time": "2024-11-14T09:46:38.856174Z", "start_time": "2024-11-14T09:46:33.520062Z" - } + }, + "tags": [] }, + "outputs": [], "source": [ "messages = [\n", " (\n", @@ -163,12 +167,11 @@ " (\"human\", \"Write a poem about Python.\"),\n", "]\n", "ai_msg = llm.invoke(messages)" - ], - "outputs": [], - "execution_count": 4 + ] }, { "cell_type": "code", + "execution_count": 5, "id": "2cd224b8-4499-41fb-a604-d53a7ff17b2e", "metadata": { "ExecuteTime": { @@ -176,9 +179,6 @@ "start_time": "2024-11-14T09:46:38.863817Z" } }, - "source": [ - "print(ai_msg.content)" - ], "outputs": [ { "name": "stdout", @@ -216,40 +216,43 @@ ] } ], - "execution_count": 5 + "source": [ + "print(ai_msg.content)" + ] }, { - "metadata": {}, "cell_type": "markdown", - "source": "## Streaming", - "id": "35b3a5b3dabef65" + "id": "35b3a5b3dabef65", + "metadata": {}, + "source": [ + "## Streaming" + ] }, { + "cell_type": "code", + "execution_count": 6, + "id": "2725770182bf96dc", "metadata": { "ExecuteTime": { "end_time": "2024-11-14T09:46:38.914883Z", "start_time": "2024-11-14T09:46:38.912564Z" } }, - "cell_type": "code", - "source": "ai_stream = llm.stream(messages)", - "id": "2725770182bf96dc", "outputs": [], - "execution_count": 6 + "source": [ + "ai_stream = llm.stream(messages)" + ] }, { + "cell_type": "code", + "execution_count": 7, + "id": "a48410d9488162e3", "metadata": { "ExecuteTime": { "end_time": "2024-11-14T09:46:43.226449Z", "start_time": "2024-11-14T09:46:38.955512Z" } }, - "cell_type": "code", - "source": [ - "for chunk in ai_stream:\n", - " print(chunk.content, end=\"\")" - ], - "id": "a48410d9488162e3", "outputs": [ { "name": "stdout", @@ -282,7 +285,10 @@ ] } ], - "execution_count": 7 + "source": [ + "for chunk in ai_stream:\n", + " print(chunk.content, end=\"\")" + ] }, { "cell_type": "markdown", @@ -296,14 +302,27 @@ }, { "cell_type": "code", + "execution_count": 8, "id": "fbb043e6", "metadata": { - "tags": [], "ExecuteTime": { "end_time": "2024-11-14T09:46:50.721645Z", "start_time": "2024-11-14T09:46:43.234590Z" - } + }, + "tags": [] }, + "outputs": [ + { + "data": { + "text/plain": [ + "AIMessageChunk(content='In the realm of code, where logic weaves and flows, \\nA language rises, like a phoenix from the code\\'s throes. \\nJava, the name, a cup of coffee\\'s steam, \\nBrewed in the minds, where digital dreams gleam.\\n\\nWith syntax clear, like morning\\'s misty hue, \\nIn classes and objects, it spins a tale so true. \\nA platform agnostic, with a byte to spare, \\nAcross the devices, it journeys everywhere.\\n\\nInheritance and polymorphism, its power\\'s core, \\nLike ancient runes, in every line they bore. \\nEncapsulation, a shield, with data it does hide, \\nIn the vast jungle of code, it stands as a guide.\\n\\nFrom applets small, to vast, server-side apps, \\nIts threads run swift, through the computing traps. \\nA language of the people, by the people, for the people’s use, \\nBuilt on the principle, \"write once, run anywhere, with no excuse.\"\\n\\nIn the heart of Android, it beats, a steady drum, \\nCrafting experiences, in every smartphone\\'s hum. \\nIn the cloud, in the enterprise, its presence is vast, \\nA cornerstone of computing, built to last.\\n\\nOh Java, thy elegance, thy robust design, \\nA language that stands, in any computing line. \\nWith every update, with every new release, \\nThy community grows, with a vibrant, diverse peace.\\n\\nSo here\\'s to Java, the versatile, the grand, \\nA language that shapes the digital land. \\nMay it continue to evolve, to grow, to inspire, \\nIn the endless quest of turning thoughts into digital fire.', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 345, 'prompt_tokens': 33, 'total_tokens': 378, 'completion_tokens_details': None, 'prompt_token_details': None}, 'model_name': 'palmyra-x-004', 'system_fingerprint': 'v1', 'finish_reason': 'stop'}, id='run-a5b4be59-0eb0-41bd-80f7-72477861b0bd-0')" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from langchain_core.prompts import ChatPromptTemplate\n", "\n", @@ -324,20 +343,7 @@ " \"input\": \"Write a poem about Java.\",\n", " }\n", ")" - ], - "outputs": [ - { - "data": { - "text/plain": [ - "AIMessageChunk(content='In the realm of code, where logic weaves and flows, \\nA language rises, like a phoenix from the code\\'s throes. \\nJava, the name, a cup of coffee\\'s steam, \\nBrewed in the minds, where digital dreams gleam.\\n\\nWith syntax clear, like morning\\'s misty hue, \\nIn classes and objects, it spins a tale so true. \\nA platform agnostic, with a byte to spare, \\nAcross the devices, it journeys everywhere.\\n\\nInheritance and polymorphism, its power\\'s core, \\nLike ancient runes, in every line they bore. \\nEncapsulation, a shield, with data it does hide, \\nIn the vast jungle of code, it stands as a guide.\\n\\nFrom applets small, to vast, server-side apps, \\nIts threads run swift, through the computing traps. \\nA language of the people, by the people, for the people’s use, \\nBuilt on the principle, \"write once, run anywhere, with no excuse.\"\\n\\nIn the heart of Android, it beats, a steady drum, \\nCrafting experiences, in every smartphone\\'s hum. \\nIn the cloud, in the enterprise, its presence is vast, \\nA cornerstone of computing, built to last.\\n\\nOh Java, thy elegance, thy robust design, \\nA language that stands, in any computing line. \\nWith every update, with every new release, \\nThy community grows, with a vibrant, diverse peace.\\n\\nSo here\\'s to Java, the versatile, the grand, \\nA language that shapes the digital land. \\nMay it continue to evolve, to grow, to inspire, \\nIn the endless quest of turning thoughts into digital fire.', additional_kwargs={}, response_metadata={'token_usage': {'completion_tokens': 345, 'prompt_tokens': 33, 'total_tokens': 378, 'completion_tokens_details': None, 'prompt_token_details': None}, 'model_name': 'palmyra-x-004', 'system_fingerprint': 'v1', 'finish_reason': 'stop'}, id='run-a5b4be59-0eb0-41bd-80f7-72477861b0bd-0')" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "execution_count": 8 + ] }, { "cell_type": "markdown", @@ -363,6 +369,7 @@ }, { "cell_type": "code", + "execution_count": 9, "id": "b7ea7690-ec7a-4337-b392-e87d1f39a6ec", "metadata": { "ExecuteTime": { @@ -370,6 +377,7 @@ "start_time": "2024-11-14T09:46:50.733463Z" } }, + "outputs": [], "source": [ "from pydantic import BaseModel, Field\n", "\n", @@ -381,12 +389,11 @@ "\n", "\n", "llm_with_tools = llm.bind_tools([GetWeather])" - ], - "outputs": [], - "execution_count": 9 + ] }, { "cell_type": "code", + "execution_count": 10, "id": "1d1ab955-6a68-42f8-bb5d-86eb1111478a", "metadata": { "ExecuteTime": { @@ -394,13 +401,12 @@ "start_time": "2024-11-14T09:46:50.904699Z" } }, + "outputs": [], "source": [ "ai_msg = llm_with_tools.invoke(\n", " \"what is the weather like in New York City\",\n", ")" - ], - "outputs": [], - "execution_count": 10 + ] }, { "cell_type": "markdown", @@ -413,6 +419,7 @@ }, { "cell_type": "code", + "execution_count": 11, "id": "166cb7ce-831d-4a7c-9721-abc107f11084", "metadata": { "ExecuteTime": { @@ -420,7 +427,6 @@ "start_time": "2024-11-14T09:46:51.738431Z" } }, - "source": "print(ai_msg.tool_calls)", "outputs": [ { "data": { @@ -436,7 +442,9 @@ "output_type": "execute_result" } ], - "execution_count": 11 + "source": [ + "print(ai_msg.tool_calls)" + ] }, { "cell_type": "markdown", diff --git a/libs/community/langchain_community/agent_toolkits/sql/base.py b/libs/community/langchain_community/agent_toolkits/sql/base.py index a738b0b948254..5cacc0f9f0a53 100644 --- a/libs/community/langchain_community/agent_toolkits/sql/base.py +++ b/libs/community/langchain_community/agent_toolkits/sql/base.py @@ -107,13 +107,13 @@ def create_sql_agent( .. code-block:: python - from langchain_openai import ChatOpenAI - from langchain_community.agent_toolkits import create_sql_agent - from langchain_community.utilities import SQLDatabase + from langchain_openai import ChatOpenAI + from langchain_community.agent_toolkits import create_sql_agent + from langchain_community.utilities import SQLDatabase - db = SQLDatabase.from_uri("sqlite:///Chinook.db") - llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0) - agent_executor = create_sql_agent(llm, db=db, agent_type="tool-calling", verbose=True) + db = SQLDatabase.from_uri("sqlite:///Chinook.db") + llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0) + agent_executor = create_sql_agent(llm, db=db, agent_type="tool-calling", verbose=True) """ # noqa: E501 from langchain.agents import ( diff --git a/libs/community/langchain_community/chat_models/perplexity.py b/libs/community/langchain_community/chat_models/perplexity.py index 88547c67d29db..ce415dd59cbd5 100644 --- a/libs/community/langchain_community/chat_models/perplexity.py +++ b/libs/community/langchain_community/chat_models/perplexity.py @@ -261,7 +261,10 @@ def _generate( response = self.client.chat.completions.create( model=params["model"], messages=message_dicts ) - message = AIMessage(content=response.choices[0].message.content) + message = AIMessage( + content=response.choices[0].message.content, + additional_kwargs={"citations": response.citations}, + ) return ChatResult(generations=[ChatGeneration(message=message)]) @property