community: fixed critical bugs at Writer provider (#27879)

This commit is contained in:
Yan
2024-11-25 20:03:37 +03:00
committed by GitHub
parent 6ed2d387bb
commit c60695a1c7
8 changed files with 1205 additions and 542 deletions

View File

@@ -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,21 +25,20 @@
]
},
{
"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",
@@ -48,15 +47,16 @@
"### 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"
"end_time": "2024-11-14T09:46:26.800627Z",
"start_time": "2024-11-14T09:27:59.652281Z"
}
},
"source": [
@@ -64,7 +64,7 @@
"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
@@ -84,23 +84,24 @@
"id": "2113471c-75d7-45df-b784-d78da4ef7aba",
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-24T13:52:49.262240Z",
"start_time": "2024-10-24T13:52:47.564879Z"
"end_time": "2024-11-14T09:46:32.415354Z",
"start_time": "2024-11-14T09:46:26.826112Z"
}
},
"source": [
"%pip install -qU langchain-community writer-sdk"
],
"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",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"execution_count": 4
"execution_count": 2
},
{
"cell_type": "markdown",
@@ -118,8 +119,8 @@
"metadata": {
"tags": [],
"ExecuteTime": {
"end_time": "2024-10-24T13:52:38.822950Z",
"start_time": "2024-10-24T13:52:38.674441Z"
"end_time": "2024-11-14T09:46:33.504711Z",
"start_time": "2024-11-14T09:46:32.574505Z"
}
},
"source": [
@@ -129,24 +130,10 @@
" 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)"
]
}
],
"outputs": [],
"execution_count": 3
},
{
@@ -159,12 +146,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce16ad78-8e6f-48cd-954e-98be75eb5836",
"metadata": {
"tags": []
"tags": [],
"ExecuteTime": {
"end_time": "2024-11-14T09:46:38.856174Z",
"start_time": "2024-11-14T09:46:33.520062Z"
}
},
"outputs": [],
"source": [
"messages = [\n",
" (\n",
@@ -173,19 +162,127 @@
" ),\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": 4
},
{
"cell_type": "code",
"execution_count": null,
"id": "2cd224b8-4499-41fb-a604-d53a7ff17b2e",
"metadata": {},
"outputs": [],
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-14T09:46:38.866651Z",
"start_time": "2024-11-14T09:46:38.863817Z"
}
},
"source": [
"print(ai_msg.content)"
]
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"In realms of code, where logic weaves and flows,\n",
"A language rises, Python by its name,\n",
"With syntax clear, where elegance it shows,\n",
"A serpent, wise, that time and space can tame.\n",
"\n",
"Born from the mind of Guido, pure and bright,\n",
"Its beauty lies in simplicity and grace,\n",
"A tool of power, yet gentle in its might,\n",
"In every programmer's heart, a cherished place.\n",
"\n",
"It dances through the data, vast and deep,\n",
"With libraries that span the digital realm,\n",
"From machine learning's secrets to keep,\n",
"To web development, it wields the helm.\n",
"\n",
"In the hands of the novice and the sage,\n",
"Python spins the threads of digital dreams,\n",
"A language that can turn the age,\n",
"With a gentle learning curve, its appeal gleams.\n",
"\n",
"It's more than code, a community it builds,\n",
"Where knowledge freely flows, and all are heard,\n",
"In Python's world, the future unfolds,\n",
"A language of the people, for the world.\n",
"\n",
"So here's to Python, in its gentle might,\n",
"A master of the modern coding art,\n",
"May it continue to light our path each night,\n",
"In the vast, evolving world of code, its heart.\n"
]
}
],
"execution_count": 5
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Streaming",
"id": "35b3a5b3dabef65"
},
{
"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
},
{
"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",
"output_type": "stream",
"text": [
"In realms of code where logic weaves,\n",
"A language rises, Python, it breezes,\n",
"With syntax clear and simple to read,\n",
"Through its elegance, our spirits are fed.\n",
"\n",
"Like rivers flowing, smooth and serene,\n",
"Its structure harmonious, a coder's dream,\n",
"Indentations guide the flow of control,\n",
"In Python's world, confusion takes no toll.\n",
"\n",
"A vast library, a treasure trove so bright,\n",
"For web and data, it offers its might,\n",
"With modules and packages, a rich array,\n",
"Python empowers us to code in play.\n",
"\n",
"From AI to scripts, in flexibility it thrives,\n",
"A language of the future, as many now derive,\n",
"Its community, a beacon of support and cheer,\n",
"With Python, the possibilities are vast, far and near.\n",
"\n",
"So here's to Python, in its gentle grace,\n",
"A tool that enhances, a language that embraces,\n",
"The art of coding, with a fluent, flowing pen,\n",
"In the Python world, we code, and we begin."
]
}
],
"execution_count": 7
},
{
"cell_type": "markdown",
@@ -199,12 +296,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "fbb043e6",
"metadata": {
"tags": []
"tags": [],
"ExecuteTime": {
"end_time": "2024-11-14T09:46:50.721645Z",
"start_time": "2024-11-14T09:46:43.234590Z"
}
},
"outputs": [],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"\n",
@@ -225,7 +324,20 @@
" \"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 peoples 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",
@@ -251,10 +363,13 @@
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b7ea7690-ec7a-4337-b392-e87d1f39a6ec",
"metadata": {},
"outputs": [],
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-14T09:46:50.891937Z",
"start_time": "2024-11-14T09:46:50.733463Z"
}
},
"source": [
"from pydantic import BaseModel, Field\n",
"\n",
@@ -266,20 +381,26 @@
"\n",
"\n",
"llm_with_tools = llm.bind_tools([GetWeather])"
]
],
"outputs": [],
"execution_count": 9
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d1ab955-6a68-42f8-bb5d-86eb1111478a",
"metadata": {},
"outputs": [],
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-14T09:46:51.725422Z",
"start_time": "2024-11-14T09:46:50.904699Z"
}
},
"source": [
"ai_msg = llm_with_tools.invoke(\n",
" \"what is the weather like in New York City\",\n",
")\n",
"ai_msg"
]
")"
],
"outputs": [],
"execution_count": 10
},
{
"cell_type": "markdown",
@@ -292,13 +413,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "166cb7ce-831d-4a7c-9721-abc107f11084",
"metadata": {},
"outputs": [],
"source": [
"ai_msg.tool_calls"
]
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-14T09:46:51.744202Z",
"start_time": "2024-11-14T09:46:51.738431Z"
}
},
"source": "print(ai_msg.tool_calls)",
"outputs": [
{
"data": {
"text/plain": [
"[{'name': 'GetWeather',\n",
" 'args': {'location': 'New York City, NY'},\n",
" 'id': 'chatcmpl-tool-fe70912c800d40fc8700d604d4823001',\n",
" 'type': 'tool_call'}]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 11
},
{
"cell_type": "markdown",

View File

@@ -4,120 +4,161 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Writer\n",
"# Writer LLM\n",
"\n",
"[Writer](https://writer.com/) is a platform to generate different language content.\n",
"\n",
"This example goes over how to use LangChain to interact with `Writer` [models](https://dev.writer.com/docs/models).\n",
"\n",
"You have to get the WRITER_API_KEY [here](https://dev.writer.com/docs)."
"## 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",
"execution_count": 4,
"metadata": {
"tags": []
"ExecuteTime": {
"end_time": "2024-11-14T11:10:46.824961Z",
"start_time": "2024-11-14T11:10:44.864137Z"
}
},
"cell_type": "code",
"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
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Installation\n",
"\n",
"The LangChain Writer integration lives in the `langchain-community` package:"
]
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-14T11:10:48.297429Z",
"start_time": "2024-11-14T11:10:46.843983Z"
}
},
"cell_type": "code",
"source": "%pip install -qU langchain-community writer-sdk",
"outputs": [
{
"name": "stdin",
"name": "stdout",
"output_type": "stream",
"text": [
" ········\n"
"\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"
]
}
],
"source": [
"from getpass import getpass\n",
"\n",
"WRITER_API_KEY = getpass()"
]
"execution_count": 2
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"WRITER_API_KEY\"] = WRITER_API_KEY"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain.chains import LLMChain\n",
"from langchain_community.llms import Writer\n",
"from langchain_core.prompts import PromptTemplate"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"template = \"\"\"Question: {question}\n",
"\n",
"Answer: Let's think step by step.\"\"\"\n",
"\n",
"prompt = PromptTemplate.from_template(template)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# If you get an error, probably, you need to set up the \"base_url\" parameter that can be taken from the error log.\n",
"\n",
"llm = Writer()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"llm_chain = LLMChain(prompt=prompt, llm=llm)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n",
"\n",
"llm_chain.run(question)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"cell_type": "markdown",
"source": "Now we can initialize our model object to interact with writer LLMs"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-14T11:10:49.818902Z",
"start_time": "2024-11-14T11:10:48.580516Z"
}
},
"cell_type": "code",
"source": [
"from langchain_community.llms import Writer as WriterLLM\n",
"\n",
"llm = WriterLLM(\n",
" temperature=0.7,\n",
" max_tokens=1000,\n",
" # other params...\n",
")"
],
"outputs": [],
"source": []
"execution_count": 3
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Invocation"
},
{
"metadata": {
"jupyter": {
"is_executing": true
},
"ExecuteTime": {
"start_time": "2024-11-14T11:10:49.832822Z"
}
},
"cell_type": "code",
"source": "response_text = llm.invoke(input=\"Write a poem\")",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": "print(response_text)",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Streaming"
},
{
"metadata": {},
"cell_type": "code",
"source": "stream_response = llm.stream(input=\"Tell me a fairytale\")",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"for chunk in stream_response:\n",
" print(chunk, end=\"\")"
],
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Async\n",
"\n",
"Writer support asynchronous calls via **ainvoke()** and **astream()** methods"
]
},
{
"metadata": {},
"cell_type": "markdown",
"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/text-generation#text-generation)."
]
}
],
"metadata": {