\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
}