Skip to content

Commit

Permalink
Merge pull request #29 from opper-ai/chris/use-call-endpoint
Browse files Browse the repository at this point in the history
Use call endpoint
  • Loading branch information
christopher-weir authored Sep 9, 2024
2 parents fadd5e9 + e985e4a commit ac37be6
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 75 deletions.
25 changes: 23 additions & 2 deletions examples/example-calls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Run example with "npx ts-node ./examples/calls.ts"
// Run example with "npx ts-node ./examples/example-calls.ts"
import "dotenv/config";

import Client from "../src";
Expand Down Expand Up @@ -36,7 +36,7 @@ const client = new Client();
name: "node-sdk/call/weather",
instructions: "Extract temperature, location and wind speed.",
input: "In London its cloudy skies early, followed by partial clearing. Cooler. High 13C. Winds ENE at 15 to 20 km/h.",
out_schema: {
output_schema: {
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: {
Expand Down Expand Up @@ -79,6 +79,27 @@ const client = new Client();
});
console.log("Weekday: ", weekday);

const { message: strawberry } = await client.call({
parent_span_uuid: trace.uuid,
name: "node-sdk/call/character-count-fewshot",
instructions:
"Carefully count the numbers of times the letter `r` exists in the supplied word",
examples: [
{
input: "runner",
output: {
thoughts:
"I need to count the number of times the character r exists in the word runner. I will go through each character: r, yes. u, no. n, no. n, no. e, no. r, yes. There are 2 occurences of the letter r in runner.",
reflection:
"I seem to have made no mistake. In the breakdown I can see that there are two occurences of the letter r",
count: 2,
},
},
],
input: "Strawberry",
});
console.log("Strawberry: ", strawberry);

await trace.end({
output: "example output",
});
Expand Down
2 changes: 1 addition & 1 deletion examples/example-indexes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Run example with "npx ts-node ./examples/indexes.ts"
// Run example with "npx ts-node ./examples/example-indexes.ts"
import "dotenv/config";
import Client from "../src";

Expand Down
2 changes: 1 addition & 1 deletion examples/example-tracing-manual.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// Run example with "npx ts-node ./examples/tracing-manual.ts"
// Run example with "npx ts-node ./examples/example-tracing-manual.ts"
import "dotenv/config";
import Client from "../src";

Expand Down
2 changes: 1 addition & 1 deletion examples/example-typebox-fn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Run example with "npx ts-node ./examples/typebox-fn-example.ts"
// Run example with "npx ts-node ./examples/example-typebox-fn.ts"
import "dotenv/config";
import { Type } from "@sinclair/typebox";

Expand Down
2 changes: 1 addition & 1 deletion examples/example-zod-fn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Run example with "npx ts-node ./examples/zod-fn-example.ts"
// Run example with "npx ts-node ./examples/example-zod-fn.ts"
import "dotenv/config";
import { z } from "zod";

Expand Down
6 changes: 2 additions & 4 deletions examples/typebox-fn-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ interface OpperOptions<InputSchema extends TSchema, OutputSchema extends TSchema
* // Create an asynchronous function using the fn factory.
* const translateAndAnalyze = fn({
* name: 'translate-and-analyze',
* description: 'Translate text and analyze sentiment.',
* instructions: 'Translate the given text to French and analyze its sentiment.',
* model: 'azure/gpt-4o-eu',
* }, InputSchema, OutputSchema);
Expand All @@ -52,7 +51,7 @@ interface OpperOptions<InputSchema extends TSchema, OutputSchema extends TSchema
* ```
*/
export default function fn<OutputSchema extends TSchema, InputSchema extends TSchema>(
{ description, instructions, model, name, examples }: OpperOptions<InputSchema, OutputSchema>,
{ instructions, model, name, examples }: OpperOptions<InputSchema, OutputSchema>,
inputSchema: InputSchema,
outputSchema: OutputSchema
): (
Expand All @@ -68,11 +67,10 @@ export default function fn<OutputSchema extends TSchema, InputSchema extends TSc
const res = await client.call({
name,
input: JSON.stringify(input),
description: description || instructions,
instructions,
model,
input_schema: inputSchema,
out_schema: outputSchema,
output_schema: outputSchema,
parent_span_uuid: options?.parent_span_uuid,
examples,
});
Expand Down
6 changes: 2 additions & 4 deletions examples/zod-fn-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ interface OpperOptions<
* // Create an asynchronous function using the fn factory.
* const translateAndAnalyze = fn({
* name: 'translate-and-analyze',
* description: 'Translate text and analyze sentiment.',
* instructions: 'Translate the given text to French and analyze its sentiment.',
* model: 'azure/gpt-4o-eu',
* }, InputSchema, OutputSchema);
Expand All @@ -58,7 +57,7 @@ export default function fn<
InputSchema extends z.ZodType<unknown, ZodTypeDef>,
OutputSchema extends z.ZodType<unknown, ZodTypeDef>,
>(
{ name, description, instructions, model, examples }: OpperOptions<InputSchema, OutputSchema>,
{ name, instructions, model, examples }: OpperOptions<InputSchema, OutputSchema>,
inputSchema: InputSchema,
outputSchema: OutputSchema
): (
Expand All @@ -74,11 +73,10 @@ export default function fn<
const res = await client.call({
name,
input: JSON.stringify(input),
description: description || instructions,
instructions,
model,
input_schema: zodToJsonSchema(inputSchema),
out_schema: zodToJsonSchema(outputSchema),
output_schema: zodToJsonSchema(outputSchema),
parent_span_uuid: options?.parent_span_uuid,
examples,
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"publish-patch": "npm run check && npm run build && npm version patch",
"publish-minor": "npm run check && npm run build && npm version minor",
"publish-major": "npm run check && npm run build && npm version major",
"run-examples": "for file in examples/example-*.ts; do echo \"Running $file\"; npx ts-node \"$file\"; echo \"\"; done"
"examples": "for file in examples/example-*.ts; do echo \"Running $file\"; npx ts-node \"$file\"; echo \"\"; done"
},
"exports": {
".": {
Expand Down
3 changes: 3 additions & 0 deletions src/api-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ class APIResource {
protected calcURLCreateFunction = () => {
return `${this._client.baseURL}/api/v1/functions`;
};
protected calcURLCall = () => {
return `${this._client.baseURL}/v1/call`;
};
protected calcURLGetFunctionByPath = (path: string) => {
return `${this._client.baseURL}/api/v1/functions/by_path/${path}`;
};
Expand Down
25 changes: 23 additions & 2 deletions src/functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpperFunction, Chat, OpperAIChatResponse, OpperAIStream } from "./types";
import { OpperFunction, OpperCall, OpperAIChatResponse, Chat, OpperAIStream } from "./types";

import APIResource from "./api-resource";
import { OpperError } from "./errors";
Expand Down Expand Up @@ -50,7 +50,6 @@ class Functions extends APIResource {
/**
* Creates a function in the OpperAI API.
* @param f - The function to be created.
* @param update - Whether to update the function if it already exists.
* @returns A promise that resolves to the created function.
* @throws {OpperError} If the function already exists and update is false.
*/
Expand All @@ -66,6 +65,28 @@ class Functions extends APIResource {
throw new OpperError(`Failed to create function: ${response.statusText}`);
}

/**
* Creates a function in the OpperAI API.
* @param f - The function to be created.
* @returns A promise that resolves to the created function.
* @throws {OpperError} If the function already exists and update is false.
*/
public async call(fn: OpperCall): Promise<OpperAIChatResponse> {
const response = await this.doPost(this.calcURLCall(), {
...fn,
input_type: fn?.input_schema,
output_type: fn?.output_schema,
});

if (response.ok) {
const data = await response.json();

return data;
}

throw new OpperError(`Failed to call function: ${response.statusText}`);
}

/**
* This method is a helper which can be used in node middleware
* to pipe the OpperAI chat stream directly to the client. See examples.
Expand Down
27 changes: 2 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { OpperAIChatResponse, OpperCall, Options } from "./types";

import { djb2 } from "./utils";
import { OpperError } from "./errors";

import Datasets from "./datasets";
Expand Down Expand Up @@ -62,30 +61,8 @@ class Client {
);
};

call = async ({
name,
input,
description,
instructions = "default",
examples,
parent_span_uuid,
...rest
}: OpperCall): Promise<OpperAIChatResponse> => {
const path = name ? name : djb2(instructions);

await this.functions.create({
path: path,
instructions: instructions,
description: description || instructions,
...rest,
});

return await this.functions.chat({
path: path,
message: input,
examples,
parent_span_uuid,
});
call = async (fn: OpperCall): Promise<OpperAIChatResponse> => {
return await this.functions.call(fn);
};
}

Expand Down
5 changes: 3 additions & 2 deletions src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Indexes extends APIResource {
public async create(name: string, embedding_model?: string): Promise<Index> {
const response = await this.doPost(this.calcURLIndexes(), {
name,
...(embedding_model && { embedding_model })
...(embedding_model && { embedding_model }),
});

const data = await response.json();
Expand Down Expand Up @@ -109,4 +109,5 @@ class Indexes extends APIResource {
return documents;
}
}
export default Indexes;

export default Indexes;
59 changes: 36 additions & 23 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,49 +100,62 @@ export type OpperExample = {
comment?: string;
};

export type BaseOpperCall = {
export type OpperCall = {
/**
* The input to the function.
* The name of the function to be used in the Opper UI.
*/
input: string;
name: string;

/**
* Description of the function for reference in the Opper UI.
* Will default to the instructions if not provided.
* The input to the function.
*/
description?: string;
input: string;

/**
* The instructions for the call sent to the model to be used as part of the prompt.
*/
instructions?: string;

/**
* The model to use to generate the output.
* See: https://docs.opper.ai/functions/models
*/
model?: string;

/**
* The input schema for the function.
*/
input_schema?: Record<string, unknown>;
out_schema?: Record<string, unknown>;

/**
* The output schema for the function.
*/
output_schema?: Record<string, unknown>;

/**
* This is used to attach to an existing span.
* If not provided, a new span will be created and displayed in the Opper UI.
*/
parent_span_uuid?: string;

/**
* Examples to use as part of the prompt to guide the model.
*/
examples?: OpperExample[];
};

export type OpperCall =
// Basic call
| (BaseOpperCall & {
name?: string;
few_shot?: never;
few_shot_count?: never;
cache_config?: never;
})
// Adavanced named function call
| (BaseOpperCall & {
name: string;
few_shot?: boolean;
few_shot_count?: number;
cache_config?: CacheConfig;
});
/**
* Whether to stream the response from the function.
*/
stream?: boolean;

configuration?: {
invocation?: {
few_shot?: {
count?: number;
};
};
};
};

export type OpperFunction = {
uuid?: string;
Expand Down
8 changes: 0 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import crypto from "node:crypto";

export function djb2(str: string) {
let hash = 5381;
for (let i = 0; i < str.length; i++) {
hash = (hash << 5) + hash + str.charCodeAt(i);
}
return Math.abs(hash).toString();
}

export const nanoId = () => {
return crypto.randomUUID();
};
Expand Down

0 comments on commit ac37be6

Please sign in to comment.