Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community: fixed critical bugs at Writer provider #27879

Merged
merged 16 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 68 additions & 80 deletions docs/docs/integrations/chat/writer.ipynb
efriis marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,28 @@
"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",
":::"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

]
},
{
"cell_type": "markdown",
"id": "e49f1e0d",
"metadata": {},
"cell_type": "markdown",
"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",
"| Class | Package | Local | Serializable | JS support | 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",
"| [Tool calling](/docs/how_to/tool_calling) | 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 |\n",
"| :---: |:-----------------:| :---: | :---: | :---: | :---: | :---: | :---: |:--------------------------------:|:--------:|\n",
"| ✅ | | ❌ | ❌ | ❌ | | ✅ | ✅ | ✅ | ❌ |\n",
"\n",
"## Setup\n",
"\n",
Expand All @@ -48,26 +47,22 @@
"### 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",
"id": "e817fe2e-4f1d-4533-b19e-2400b1cf6ce8",
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-24T13:51:54.323678Z",
"start_time": "2024-10-24T13:51:42.127404Z"
}
},
"metadata": {},
"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: \")"
" os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key:\")"
],
"outputs": [],
"execution_count": 1
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -82,25 +77,10 @@
{
"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
"metadata": {},
"source": "%pip install -qU langchain-community writer-sdk",
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -116,38 +96,23 @@
"cell_type": "code",
"id": "522686de",
"metadata": {
"tags": [],
"ExecuteTime": {
"end_time": "2024-10-24T13:52:38.822950Z",
"start_time": "2024-10-24T13:52:38.674441Z"
}
"tags": []
},
"source": [
"from langchain_community.chat_models.writer import ChatWriter\n",
"from writerai import AsyncWriter, Writer\n",
"\n",
"llm = ChatWriter(\n",
" client=Writer(),\n",
" async_client=AsyncWriter(),\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
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -159,12 +124,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce16ad78-8e6f-48cd-954e-98be75eb5836",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"messages = [\n",
" (\n",
Expand All @@ -173,19 +136,45 @@
" ),\n",
" (\"human\", \"Write a poem about Python.\"),\n",
"]\n",
"ai_msg = llm.invoke(messages)\n",
"ai_msg"
]
"ai_msg = llm.invoke(messages)"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"execution_count": null,
"id": "2cd224b8-4499-41fb-a604-d53a7ff17b2e",
"metadata": {},
"outputs": [],
"source": [
"print(ai_msg.content)"
]
],
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Streaming",
"id": "35b3a5b3dabef65"
},
{
"metadata": {},
"cell_type": "code",
"source": "ai_stream = llm.stream(messages)",
"id": "2725770182bf96dc",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"for chunk in ai_stream:\n",
" print(chunk.content, end=\"\")"
],
"id": "a48410d9488162e3",
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -199,12 +188,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "fbb043e6",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"\n",
Expand All @@ -225,7 +212,9 @@
" \"input\": \"Write a poem about Java.\",\n",
" }\n",
")"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -251,10 +240,8 @@
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b7ea7690-ec7a-4337-b392-e87d1f39a6ec",
"metadata": {},
"outputs": [],
"source": [
"from pydantic import BaseModel, Field\n",
"\n",
Expand All @@ -266,20 +253,21 @@
"\n",
"\n",
"llm_with_tools = llm.bind_tools([GetWeather])"
]
],
"outputs": [],
"execution_count": null
},
{
"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"
]
")"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
Expand All @@ -292,13 +280,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "166cb7ce-831d-4a7c-9721-abc107f11084",
"metadata": {},
"outputs": [],
"source": [
"ai_msg.tool_calls"
]
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
Expand Down
Loading
Loading