Skip to content

Commit

Permalink
)test: fixed tests acording new init proccess
Browse files Browse the repository at this point in the history
  • Loading branch information
yanomaly committed Nov 14, 2024
1 parent 0d0e505 commit 6040d33
Show file tree
Hide file tree
Showing 3 changed files with 430 additions and 286 deletions.
214 changes: 182 additions & 32 deletions docs/docs/integrations/chat/writer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@
{
"cell_type": "code",
"id": "e817fe2e-4f1d-4533-b19e-2400b1cf6ce8",
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-14T09:46:26.800627Z",
"start_time": "2024-11-14T09:27:59.652281Z"
}
},
"source": [
"import getpass\n",
"import os\n",
Expand All @@ -62,7 +67,7 @@
" os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key:\")"
],
"outputs": [],
"execution_count": null
"execution_count": 1
},
{
"cell_type": "markdown",
Expand All @@ -77,10 +82,26 @@
{
"cell_type": "code",
"id": "2113471c-75d7-45df-b784-d78da4ef7aba",
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-14T09:46:32.415354Z",
"start_time": "2024-11-14T09:46:26.826112Z"
}
},
"source": "%pip install -qU langchain-community writer-sdk",
"outputs": [],
"execution_count": null
"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": 2
},
{
"cell_type": "markdown",
Expand All @@ -96,23 +117,24 @@
"cell_type": "code",
"id": "522686de",
"metadata": {
"tags": []
"tags": [],
"ExecuteTime": {
"end_time": "2024-11-14T09:46:33.504711Z",
"start_time": "2024-11-14T09:46:32.574505Z"
}
},
"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",
" # other params...\n",
")"
],
"outputs": [],
"execution_count": null
"execution_count": 3
},
{
"cell_type": "markdown",
Expand All @@ -126,7 +148,11 @@
"cell_type": "code",
"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"
}
},
"source": [
"messages = [\n",
Expand All @@ -139,17 +165,58 @@
"ai_msg = llm.invoke(messages)"
],
"outputs": [],
"execution_count": null
"execution_count": 4
},
{
"cell_type": "code",
"id": "2cd224b8-4499-41fb-a604-d53a7ff17b2e",
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2024-11-14T09:46:38.866651Z",
"start_time": "2024-11-14T09:46:38.863817Z"
}
},
"source": [
"print(ai_msg.content)"
],
"outputs": [],
"execution_count": null
"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": {},
Expand All @@ -158,23 +225,64 @@
"id": "35b3a5b3dabef65"
},
{
"metadata": {},
"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": null
"execution_count": 6
},
{
"metadata": {},
"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": [],
"execution_count": null
"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",
Expand All @@ -190,7 +298,11 @@
"cell_type": "code",
"id": "fbb043e6",
"metadata": {
"tags": []
"tags": [],
"ExecuteTime": {
"end_time": "2024-11-14T09:46:50.721645Z",
"start_time": "2024-11-14T09:46:43.234590Z"
}
},
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
Expand All @@ -213,8 +325,19 @@
" }\n",
")"
],
"outputs": [],
"execution_count": null
"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",
Expand All @@ -241,7 +364,12 @@
{
"cell_type": "code",
"id": "b7ea7690-ec7a-4337-b392-e87d1f39a6ec",
"metadata": {},
"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",
Expand All @@ -255,19 +383,24 @@
"llm_with_tools = llm.bind_tools([GetWeather])"
],
"outputs": [],
"execution_count": null
"execution_count": 9
},
{
"cell_type": "code",
"id": "1d1ab955-6a68-42f8-bb5d-86eb1111478a",
"metadata": {},
"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",
")"
],
"outputs": [],
"execution_count": null
"execution_count": 10
},
{
"cell_type": "markdown",
Expand All @@ -281,12 +414,29 @@
{
"cell_type": "code",
"id": "166cb7ce-831d-4a7c-9721-abc107f11084",
"metadata": {},
"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"
}
],
"outputs": [],
"execution_count": null
"execution_count": 11
},
{
"cell_type": "markdown",
Expand Down
Loading

0 comments on commit 6040d33

Please sign in to comment.