Skip to content

Conversation

@andreibratu
Copy link

No description provided.

@andreibratu andreibratu force-pushed the ENG-1417-ts-sdk-utilities branch from 0d33f55 to 3d38f5e Compare December 10, 2024 15:14
@andreibratu
Copy link
Author

Tested with the following script. DM for details on linking the local version of the SDK to the script

import { HumanloopClient } from "humanloop/dist/index";
import * as dotenv from "dotenv";
import * as readline from "readline/promises";
import { FunctionDefinition } from "openai/resources";

dotenv.config({
  path: __dirname + "/.env",
});

const humanloop = new HumanloopClient({
  apiKey: process.env.HUMANLOOP_KEY || "",
});
import OpenAI from "openai";
const openAIClient = new OpenAI({ apiKey: process.env.OPENAI_KEY });

const calculator = humanloop.tool({
  callable: (operation: string, num1: number, num2: number) => {
    switch (operation) {
      case "add":
        return num1 + num2;
      case "subtract":
        return num1 - num2;
      case "multiply":
        return num1 * num2;
      case "divide":
        if (num2 === 0) {
          throw new Error("Cannot divide by zero");
        }
        return num1 / num2;
      default:
        throw new Error("Invalid operation");
    }
  },
  toolKernel: {
    function: {
      name: "calculator",
      description: "Perform arithmetic operations on two numbers",
      strict: true,
      parameters: {
        type: "object",
        properties: {
          operation: {
            type: "string",
            description: "The operation to perform",
            enum: ["add", "subtract", "multiply", "divide"],
          },
          num1: {
            type: "number",
            description: "The first number",
          },
          num2: {
            type: "number",
            description: "The second number",
          },
        },
        required: ["operation", "num1", "num2"],
        additionalProperties: false,
      },
    },
  },
  path: "Andrei QA/TS Utilities/Calculator",
});

const callModel = humanloop.prompt({
  callable: async (
    messages: { content: string; role: "system" | "user" | "assistant" }[]
  ) => {
    const output = await openAIClient.chat.completions.create({
      model: "gpt-4o",
      temperature: 0.8,
      messages: messages,
      tools: [
        {
          type: "function",
          function: calculator.jsonSchema as FunctionDefinition,
        },
      ],
    });

    if (output.choices[0].message.tool_calls) {
      for (const toolCall of output.choices[0].message.tool_calls) {
        const toolCallArgs = JSON.parse(toolCall.function.arguments);
        const result = await calculator(
          toolCallArgs.operation,
          toolCallArgs.num1,
          toolCallArgs.num2
        );
        return `[TOOL CALL] ${result}`;
      }
    }

    return output.choices[0].message.content || "";
  },
  path: "Andrei QA/TS Utilities/Call Model",
});

const conversation = humanloop.flow({
  callable: async () => {
    const messages = [
      {
        role: "system",
        content:
          "You are a groovy 80s surfer dude helping with math and science.",
      },
    ];
    const rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout,
    });
    while (true) {
      let userInput = await rl.question("You: ");
      if (userInput === "exit") {
        rl.close();
        break;
      }
      messages.push({ role: "user", content: userInput });
      const response = await callModel(messages);
      messages.push({
        role: "assistant",
        content: response,
      });
      console.log("Assistant:", response);
    }
  },
  path: "Andrei QA/TS Utilities/Conversation",
});

conversation();

package.json Outdated
"ts-json-schema-generator": "^2.3.0",
"url-join": "4.0.1",
"uuid": "^11.0.3",
"yarn": "^1.22.22"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"yarn": "^1.22.22"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack fixed

@andreibratu andreibratu merged commit 5f57e74 into master Dec 20, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants