-
Notifications
You must be signed in to change notification settings - Fork 0
Fixes ENG-1417: TS SDK Utilities #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0d33f55 to
3d38f5e
Compare
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" |
Contributor
There was a problem hiding this comment.
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" |
Author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack fixed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.