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 all 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
268 changes: 203 additions & 65 deletions docs/docs/integrations/chat/writer.ipynb

Large diffs are not rendered by default.

171 changes: 106 additions & 65 deletions docs/docs/integrations/llms/writer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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:"
]
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-14T11:10:46.824961Z",
"start_time": "2024-11-14T11:10:44.864137Z"
}
},
"cell_type": "code",
"execution_count": 4,
"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": {
"tags": []
"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
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Now we can initialize our model object to interact with writer LLMs"
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"tags": []
"ExecuteTime": {
"end_time": "2024-11-14T11:10:49.818902Z",
"start_time": "2024-11-14T11:10:48.580516Z"
}
},
"outputs": [],
"cell_type": "code",
"source": [
"import os\n",
"from langchain_community.llms import Writer as WriterLLM\n",
"\n",
"os.environ[\"WRITER_API_KEY\"] = WRITER_API_KEY"
]
"llm = WriterLLM(\n",
" temperature=0.7,\n",
" max_tokens=1000,\n",
" # other params...\n",
")"
],
"outputs": [],
"execution_count": 3
},
{
"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"
]
"metadata": {},
"cell_type": "markdown",
"source": "## Invocation"
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"tags": []
"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": [],
"source": [
"template = \"\"\"Question: {question}\n",
"\n",
"Answer: Let's think step by step.\"\"\"\n",
"\n",
"prompt = PromptTemplate.from_template(template)"
]
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"execution_count": 14,
"metadata": {
"tags": []
},
"source": "print(response_text)",
"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()"
]
"execution_count": null
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Streaming"
},
{
"metadata": {},
"cell_type": "code",
"execution_count": 15,
"metadata": {
"tags": []
},
"source": "stream_response = llm.stream(input=\"Tell me a fairytale\")",
"outputs": [],
"source": [
"llm_chain = LLMChain(prompt=prompt, llm=llm)"
]
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"source": [
"for chunk in stream_response:\n",
" print(chunk, end=\"\")"
],
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n",
"## Async\n",
"\n",
"llm_chain.run(question)"
"Writer support asynchronous calls via **ainvoke()** and **astream()** methods"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"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": {
Expand Down
Loading
Loading