datagusto-dev-sdk
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

Datagusto SDK for JavaScript/TypeScript

This is the official JavaScript/TypeScript SDK for Datagusto AI platform.

Installation

You can install the package using npm:

npm install datagusto-dev-sdk

Or using yarn:

yarn add datagusto-dev-sdk

Quick Start

import { ChatOpenAI } from "@langchain/openai";
import { StateGraph, Annotation } from "@langchain/langgraph";
import { z } from "zod";
import * as process from 'process';
import { LangchainCallbackHandler } from "./src/callback.js";


process.env.OPENAI_API_KEY = "";
process.env.DATAGUSTO_SECRET_KEY = "";

const handler = new LangchainCallbackHandler({});

const llm = new ChatOpenAI({
  modelName: "gpt-4o-mini",
});


// Schema for structured output to use as routing logic
const routeSchema = z.object({
  step: z.enum(["poem", "story", "joke"]).describe(
    "The next step in the routing process"
  ),
});

// Augment the LLM with schema for structured output
const router = llm.withStructuredOutput(routeSchema);

// Graph state
const StateAnnotation = Annotation.Root({
  input: Annotation<string>,
  decision: Annotation<string>,
  output: Annotation<string>,
});

// Nodes
// Write a story
async function llmCall1(state: typeof StateAnnotation.State) {
  const result = await llm.invoke([{
    role: "system",
    content: "You are an expert storyteller.",
  }, {
    role: "user",
    content: state.input
  }]);
  return { output: result.content };
}

// Write a joke
async function llmCall2(state: typeof StateAnnotation.State) {
  const result = await llm.invoke([{
    role: "system",
    content: "You are an expert comedian.",
  }, {
    role: "user",
    content: state.input
  }]);
  return { output: result.content };
}

// Write a poem
async function llmCall3(state: typeof StateAnnotation.State) {
  const result = await llm.invoke([{
    role: "system",
    content: "You are an expert poet.",
  }, {
    role: "user",
    content: state.input
  }]);
  return { output: result.content };
}

async function llmCallRouter(state: typeof StateAnnotation.State) {
  // Route the input to the appropriate node
  const decision = await router.invoke([
    {
      role: "system",
      content: "Route the input to story, joke, or poem based on the user's request."
    },
    {
      role: "user",
      content: state.input
    },
  ]);

  return { decision: decision.step };
}

// Conditional edge function to route to the appropriate node
function routeDecision(state: typeof StateAnnotation.State) {
  // Return the node name you want to visit next
  if (state.decision === "story") {
    return "llmCall1";
  } else if (state.decision === "joke") {
    return "llmCall2";
  } else if (state.decision === "poem") {
    return "llmCall3";
  } else {
    // デフォルトの振る舞い
    return "llmCall1";
  }
}

// Build workflow
const routerWorkflow = new StateGraph(StateAnnotation)
  .addNode("llmCall1", llmCall1)
  .addNode("llmCall2", llmCall2)
  .addNode("llmCall3", llmCall3)
  .addNode("llmCallRouter", llmCallRouter)
  .addEdge("__start__", "llmCallRouter")
  .addConditionalEdges(
    "llmCallRouter",
    routeDecision,
    ["llmCall1", "llmCall2", "llmCall3"],
  )
  .addEdge("llmCall1", "__end__")
  .addEdge("llmCall2", "__end__")
  .addEdge("llmCall3", "__end__")
  .compile().withConfig({
    callbacks: [handler]
  });

// Invoke
const state = await routerWorkflow.invoke({
  input: "Write me a joke about cats"
});
console.log(state.output);

Requirements

  • Node.js 18 or later
  • Dependencies:
    • @langchain/core
    • langchain

License

MIT License

Package Sidebar

Install

npm i datagusto-dev-sdk

Homepage

datagusto.ai

Weekly Downloads

9

Version

0.1.2

License

MIT

Unpacked Size

35.2 kB

Total Files

8

Last publish

Collaborators

  • nkmrtty