Skip to content

Commit

Permalink
docs(js): add langchain deno notebook (#4848)
Browse files Browse the repository at this point in the history
  • Loading branch information
Parker-Stafford authored Oct 7, 2024
1 parent 5d667d5 commit 9bb57b7
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 9 deletions.
3 changes: 3 additions & 0 deletions js/examples/notebooks/langchain/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"nodeModulesDir": "manual"
}
11 changes: 11 additions & 0 deletions js/examples/notebooks/langchain/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"dependencies": {
"@arizeai/openinference-instrumentation-langchain": "^0.2.0",
"@arizeai/openinference-semantic-conventions": "^0.10.0",
"@langchain/core": "^0.2.30",
"@langchain/openai": "^0.2.0",
"@opentelemetry/exporter-trace-otlp-proto": "^0.50.0",
"@opentelemetry/resources": "^1.25.1",
"@opentelemetry/sdk-trace-node": "^1.25.1"
}
}
124 changes: 124 additions & 0 deletions js/examples/notebooks/langchain/tracing_langchain_node_tutorial.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
" <p style=\"text-align:center\">\n",
" <img alt=\"phoenix logo\" src=\"https://raw.githubusercontent.com/Arize-ai/phoenix-assets/9e6101d95936f4bd4d390efc9ce646dc6937fb2d/images/socal/github-large-banner-phoenix.jpg\" width=\"1000\"/>\n",
" <br>\n",
" <br>\n",
" <a href=\"https://docs.arize.com/phoenix/\">Docs</a>\n",
" |\n",
" <a href=\"https://github.com/Arize-ai/phoenix\">GitHub</a>\n",
" |\n",
" <a href=\"https://join.slack.com/t/arize-ai/shared_invite/zt-1px8dcmlf-fmThhDFD_V_48oU7ALan4Q\">Community</a>\n",
" </p>\n",
"</center>\n",
"<h1 align=\"center\">LangChain Tracing</h1>\n",
"\n",
"Let's see how to get started with [LangChain.js](https://js.langchain.com/docs/introduction/) and [OpenInference](https://github.com/Arize-ai/openinference/tree/main/js) to trace your LangChain application using Deno.\n",
"\n",
"> Note: that this example requires the OPENAI_API_KEY environment variable to be set and assumes you are running the Phoenix server on localhost:6006.\n",
"\n",
"In order to run this notebook please run the following command from this directory:\n",
"```shell\n",
"npm install\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import {\n",
" NodeTracerProvider,\n",
" SimpleSpanProcessor,\n",
"} from \"@opentelemetry/sdk-trace-node\";\n",
"import { Resource } from \"@opentelemetry/resources\";\n",
"import { OTLPTraceExporter } from \"@opentelemetry/exporter-trace-otlp-proto\";\n",
"import { SEMRESATTRS_PROJECT_NAME } from \"@arizeai/openinference-semantic-conventions\";\n",
"\n",
"const provider = new NodeTracerProvider({\n",
" resource: new Resource({\n",
" [SEMRESATTRS_PROJECT_NAME]: \"deno-langchain\",\n",
" }),\n",
"});\n",
"\n",
"provider.addSpanProcessor(\n",
" new SimpleSpanProcessor(\n",
" new OTLPTraceExporter({\n",
" url: \"http://localhost:6006/v1/traces\",\n",
" })\n",
" )\n",
");\n",
"\n",
"provider.register();\n",
"\n",
"console.log(\"👀 OpenInference initialized\");\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import * as lcCallbackManager from \"@langchain/core/callbacks/manager\";\n",
"import { LangChainInstrumentation } from \"@arizeai/openinference-instrumentation-langchain\";\n",
"\n",
"const lcInstrumentation = new LangChainInstrumentation();\n",
"lcInstrumentation.manuallyInstrument(lcCallbackManager);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import { ChatOpenAI } from \"@langchain/openai\";\n",
"import { ChatPromptTemplate } from \"@langchain/core/prompts\";\n",
"\n",
"const PROMPT_TEMPLATE = `\n",
" You are a highly trained AI model that can answer any question. Please answer questions concisely. \n",
" Answer the following question: \n",
" \"{question}\"\n",
"`;\n",
"\n",
"const question = \"What is javascript used for?\";\n",
"\n",
"const chatPrompt = ChatPromptTemplate.fromTemplate(PROMPT_TEMPLATE)\n",
"\n",
"const model = new ChatOpenAI({ model: \"gpt-4\" });\n",
"\n",
"const chain = chatPrompt.pipe(model);\n",
"\n",
"const response = await chain.invoke({ question });\n",
"\n",
"console.log(response.content);\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Deno",
"language": "typescript",
"name": "deno"
},
"language_info": {
"codemirror_mode": "typescript",
"file_extension": ".ts",
"mimetype": "text/x.typescript",
"name": "typescript",
"nbconvert_exporter": "script",
"pygments_lexer": "typescript",
"version": "5.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
13 changes: 4 additions & 9 deletions js/examples/notebooks/tracing_openai_node_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"</center>\n",
"<h1 align=\"center\">OpenAI Node SDK Tracing</h1>\n",
"\n",
"Let's see how to get started with using the OpenAI Node SDK to trace your LLM calls using Deno. Note that this example requires the OPENAI_API_KEY environment variable to be set and assumes you are running the Phoenix server on localhost:6006."
"Let's see how to get started with using the OpenAI Node SDK to trace your LLM calls using Deno. \n",
"\n",
"> Note: that this example requires the OPENAI_API_KEY environment variable to be set and assumes you are running the Phoenix server on localhost:6006."
]
},
{
Expand Down Expand Up @@ -60,7 +62,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -95,13 +97,6 @@
"\n",
"await main();"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 9bb57b7

Please sign in to comment.