-
Notifications
You must be signed in to change notification settings - Fork 16k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
community: Add Writer integration (#27646)
**Description:** Add support for Writer chat models **Issue:** N/A **Dependencies:** Add `writer-sdk` to optional dependencies. **Twitter handle:** Please tag `@samjulien` and `@Get_Writer` **Tests and docs** - [x] Unit test - [x] Example notebook in `docs/docs/integrations` directory. **Lint and test** - [x] Run `make format` - [x] Run `make lint` - [x] Run `make test` --------- Co-authored-by: Johannes <[email protected]> Co-authored-by: Erick Friis <[email protected]>
- Loading branch information
1 parent
595dc59
commit 0a472e2
Showing
4 changed files
with
965 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,343 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"metadata": {}, | ||
"cell_type": "raw", | ||
"source": [ | ||
"---\n", | ||
"sidebar_label: Writer\n", | ||
"---" | ||
], | ||
"id": "85e07aae70a15572" | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "cb4dd00a-8893-4a45-96f7-9a9fc341cd61", | ||
"metadata": {}, | ||
"source": [ | ||
"# ChatWriter\n", | ||
"\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", | ||
":::" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e49f1e0d", | ||
"metadata": {}, | ||
"source": [ | ||
"## Overview\n", | ||
"\n", | ||
"### Integration details\n", | ||
"| Class | Package | Local | Serializable | [JS support](https://js.langchain.com/docs/integrations/chat/openai) | Package downloads | Package latest |\n", | ||
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n", | ||
"| ChatWriter | langchain-community | ❌ | ❌ | ❌ | ❌ | ❌ |\n", | ||
"\n", | ||
"### Model features\n", | ||
"| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | Image input | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n", | ||
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n", | ||
"| ✅ | ❌ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | \n", | ||
"\n", | ||
"## Setup\n", | ||
"\n", | ||
"To access Writer models you'll need to create a Writer account, get an API key, and install the `writer-sdk` and `langchain-community` packages.\n", | ||
"\n", | ||
"### 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:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"id": "e817fe2e-4f1d-4533-b19e-2400b1cf6ce8", | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-10-24T13:51:54.323678Z", | ||
"start_time": "2024-10-24T13:51:42.127404Z" | ||
} | ||
}, | ||
"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", | ||
"id": "c59722a9-6dbb-45f7-ae59-5be50ca5733d", | ||
"metadata": {}, | ||
"source": [ | ||
"### Installation\n", | ||
"\n", | ||
"The LangChain Writer integration lives in the `langchain-community` package:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"id": "2113471c-75d7-45df-b784-d78da4ef7aba", | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-10-24T13:52:49.262240Z", | ||
"start_time": "2024-10-24T13:52:47.564879Z" | ||
} | ||
}, | ||
"source": [ | ||
"%pip install -qU langchain-community writer-sdk" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Note: you may need to restart the kernel to use updated packages.\n" | ||
] | ||
} | ||
], | ||
"execution_count": 4 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "1098bc9d-ce83-462b-8c19-f85bf3a159dc", | ||
"metadata": {}, | ||
"source": [ | ||
"## Instantiation\n", | ||
"\n", | ||
"Now we can instantiate our model object and generate chat completions:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"id": "522686de", | ||
"metadata": { | ||
"tags": [], | ||
"ExecuteTime": { | ||
"end_time": "2024-10-24T13:52:38.822950Z", | ||
"start_time": "2024-10-24T13:52:38.674441Z" | ||
} | ||
}, | ||
"source": [ | ||
"from langchain_community.chat_models.writer import ChatWriter\n", | ||
"\n", | ||
"llm = ChatWriter(\n", | ||
" model=\"palmyra-x-004\",\n", | ||
" temperature=0.7,\n", | ||
" max_tokens=1000,\n", | ||
" # api_key=\"...\", # if you prefer to pass api key in directly instaed of using env vars\n", | ||
" # base_url=\"...\",\n", | ||
" # other params...\n", | ||
")" | ||
], | ||
"outputs": [ | ||
{ | ||
"ename": "ImportError", | ||
"evalue": "cannot import name 'ChatWriter' from 'langchain_community.chat_models' (/home/yanomaly/PycharmProjects/whitesnake/writer/langсhain/libs/community/langchain_community/chat_models/__init__.py)", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", | ||
"\u001B[0;31mImportError\u001B[0m Traceback (most recent call last)", | ||
"Cell \u001B[0;32mIn[3], line 1\u001B[0m\n\u001B[0;32m----> 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mlangchain_community\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mchat_models\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m ChatWriter\n\u001B[1;32m 3\u001B[0m llm \u001B[38;5;241m=\u001B[39m ChatWriter(\n\u001B[1;32m 4\u001B[0m model\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mpalmyra-x-004\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[1;32m 5\u001B[0m temperature\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m0.7\u001B[39m,\n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 9\u001B[0m \u001B[38;5;66;03m# other params...\u001B[39;00m\n\u001B[1;32m 10\u001B[0m )\n", | ||
"\u001B[0;31mImportError\u001B[0m: cannot import name 'ChatWriter' from 'langchain_community.chat_models' (/home/yanomaly/PycharmProjects/whitesnake/writer/langсhain/libs/community/langchain_community/chat_models/__init__.py)" | ||
] | ||
} | ||
], | ||
"execution_count": 3 | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "6511982a-734a-4193-a47d-254f8dcaff5e", | ||
"metadata": {}, | ||
"source": [ | ||
"## Invocation" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "ce16ad78-8e6f-48cd-954e-98be75eb5836", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"messages = [\n", | ||
" (\n", | ||
" \"system\",\n", | ||
" \"You are a helpful assistant that writes poems about the Python programming language.\",\n", | ||
" ),\n", | ||
" (\"human\", \"Write a poem about Python.\"),\n", | ||
"]\n", | ||
"ai_msg = llm.invoke(messages)\n", | ||
"ai_msg" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "2cd224b8-4499-41fb-a604-d53a7ff17b2e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"print(ai_msg.content)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "778f912a-66ea-4a5d-b3de-6c7db4baba26", | ||
"metadata": {}, | ||
"source": [ | ||
"## Chaining\n", | ||
"\n", | ||
"We can [chain](/docs/how_to/sequence/) our model with a prompt template like so:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "fbb043e6", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain_core.prompts import ChatPromptTemplate\n", | ||
"\n", | ||
"prompt = ChatPromptTemplate.from_messages(\n", | ||
" [\n", | ||
" (\n", | ||
" \"system\",\n", | ||
" \"You are a helpful assistant that writes poems about the {input_language} programming language.\",\n", | ||
" ),\n", | ||
" (\"human\", \"{input}\"),\n", | ||
" ]\n", | ||
")\n", | ||
"\n", | ||
"chain = prompt | llm\n", | ||
"chain.invoke(\n", | ||
" {\n", | ||
" \"input_language\": \"Java\",\n", | ||
" \"input\": \"Write a poem about Java.\",\n", | ||
" }\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "0b1b52a5-b58d-40c9-bcdd-88eb8fb351e2", | ||
"metadata": {}, | ||
"source": [ | ||
"## Tool calling\n", | ||
"\n", | ||
"Writer supports [tool calling](https://dev.writer.com/api-guides/tool-calling), which lets you describe tools and their arguments, and have the model return a JSON object with a tool to invoke and the inputs to that tool.\n", | ||
"\n", | ||
"### ChatWriter.bind_tools()\n", | ||
"\n", | ||
"With `ChatWriter.bind_tools`, we can easily pass in Pydantic classes, dict schemas, LangChain tools, or even functions as tools to the model. Under the hood these are converted to tool schemas, which looks like:\n", | ||
"```\n", | ||
"{\n", | ||
" \"name\": \"...\",\n", | ||
" \"description\": \"...\",\n", | ||
" \"parameters\": {...} # JSONSchema\n", | ||
"}\n", | ||
"```\n", | ||
"and passed in every model invocation." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "b7ea7690-ec7a-4337-b392-e87d1f39a6ec", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pydantic import BaseModel, Field\n", | ||
"\n", | ||
"\n", | ||
"class GetWeather(BaseModel):\n", | ||
" \"\"\"Get the current weather in a given location\"\"\"\n", | ||
"\n", | ||
" location: str = Field(..., description=\"The city and state, e.g. San Francisco, CA\")\n", | ||
"\n", | ||
"\n", | ||
"llm_with_tools = llm.bind_tools([GetWeather])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "1d1ab955-6a68-42f8-bb5d-86eb1111478a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"ai_msg = llm_with_tools.invoke(\n", | ||
" \"what is the weather like in New York City\",\n", | ||
")\n", | ||
"ai_msg" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "768d1ae4-4b1a-48eb-a329-c8d5051067a3", | ||
"metadata": {}, | ||
"source": [ | ||
"### AIMessage.tool_calls\n", | ||
"Notice that the AIMessage has a `tool_calls` attribute. This contains in a standardized ToolCall format that is model-provider agnostic." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "166cb7ce-831d-4a7c-9721-abc107f11084", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"ai_msg.tool_calls" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e082c9ac-c7c7-4aff-a8ec-8e220262a59c", | ||
"metadata": {}, | ||
"source": [ | ||
"For more on binding tools and tool call outputs, head to the [tool calling](/docs/how_to/function_calling) docs." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a796d728-971b-408b-88d5-440015bbb941", | ||
"metadata": {}, | ||
"source": [ | ||
"## API reference\n", | ||
"\n", | ||
"For detailed documentation of all Writer features, head to our [API reference](https://dev.writer.com/api-guides/api-reference/completion-api/chat-completion)." | ||
] | ||
} | ||
], | ||
"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.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.