Skip to content

Commit

Permalink
rename sdk functions/params based on discussions (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
xke committed Apr 22, 2024
1 parent a56317a commit 3d86904
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 39 deletions.
6 changes: 6 additions & 0 deletions .changeset/six-penguins-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@examples/playground": minor
"@gentrace/playground": minor
---

rename sdk functions/params to be clearer
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ yarn-error.log
tsconfig.tsbuildinfo
.idea

/examples/playground/src/movies
39 changes: 20 additions & 19 deletions examples/playground/src/example-interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,45 @@ function createInterpolatedMessages(template: object, inputs: object): string {
async function summarizeTextOpenAI(
stepName: string,
method: string,
args: object,
inputs: object,
args: Record<string, any>,
inputs: Record<string, any>,
): Promise<string> {
if (method !== "openai.chat.completions.create") {
throw new Error("Method " + method + " is not supported.");
}

const { newArgs, id, cachedOutput } = gentrace.getStepInfo(
stepName,
method,
args,
inputs,
);
const { overrideArgsTemplate, stepRunId, cachedResponse } =
gentrace.getStepRun(stepName, method, args, inputs);

if (cachedOutput && cachedOutput.length > 0) {
console.log("CACHE: using cachedOutput: " + cachedOutput);
gentrace.submitOutput(id, cachedOutput);
return cachedOutput;
if (cachedResponse && cachedResponse.length > 0) {
console.log("CACHE: using cachedResponse: " + cachedResponse);
gentrace.submitStepRun(stepRunId, cachedResponse);
return cachedResponse;
}

console.log(
"interpolatedMessage: " +
JSON.stringify(createInterpolatedMessages(newArgs.messages, inputs)),
JSON.stringify(
createInterpolatedMessages(overrideArgsTemplate.messages, inputs),
),
);

const messages: any = createInterpolatedMessages(newArgs.messages, inputs);
const messages: any = createInterpolatedMessages(
overrideArgsTemplate.messages,
inputs,
);

try {
const completion = await openai.chat.completions.create({
model: newArgs.model, // gpt-3.5-turbo, gpt-4, etc.
model: overrideArgsTemplate.model, // gpt-3.5-turbo, gpt-4, etc.
messages: messages,
temperature: newArgs.temperature,
temperature: overrideArgsTemplate.temperature,
});

const output = completion.choices[0].message.content || "";
gentrace.submitOutput(id, output);
const response = completion.choices[0].message.content || "";
gentrace.submitStepRun(stepRunId, response);

return output;
return response;
} catch (error) {
console.error("Error:", error);
}
Expand Down
40 changes: 20 additions & 20 deletions packages/playground/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,24 +292,24 @@ export class GentraceSession {
});
}

public getStepInfo<T extends Record<string, any> = Record<string, any>>(
public getStepRun<T extends Record<string, any> = Record<string, any>>(
stepName: string,
stepMethod: string,
defaultArgs: T,
interpolationVariables?: Record<string, any>,
): { newArgs: T; id: string; cachedOutput?: string } {
method: string,
argsTemplate: T,
templateData?: Record<string, any>,
): { overrideArgsTemplate: T; stepRunId: string; cachedResponse?: string } {
const store = asyncLocalStorage.getStore() as any;

const stepOverrides = store.get("stepOverrides");

console.log("stepOverrides: ");
console.log(stepOverrides);

let newInputArgs = defaultArgs;
let newArgsTemplate = argsTemplate;

for (const stepOverride of stepOverrides) {
if (stepOverride.name == stepName) {
Object.assign(newInputArgs, stepOverride.overrides);
Object.assign(newArgsTemplate, stepOverride.overrides);
}
}

Expand All @@ -323,14 +323,14 @@ export class GentraceSession {
store.get("stepsArray").push({
id: stepId,
name: stepName,
method: stepMethod,
inputs: newInputArgs,
interpolation: interpolationVariables,
method: method,
inputs: newArgsTemplate,
interpolation: templateData,
});

const cachedInputString = this.getCachedInputString(
newInputArgs,
interpolationVariables,
newArgsTemplate,
templateData,
);

cachedStepInputs.set(stepId, cachedInputString);
Expand All @@ -341,22 +341,22 @@ export class GentraceSession {
}

return {
newArgs: newInputArgs,
id: stepId,
cachedOutput: cachedOutput,
overrideArgsTemplate: newArgsTemplate,
stepRunId: stepId,
cachedResponse: cachedOutput,
};
}

private isOutputValid(output: string): boolean {
return output.trim().length > 0;
}

public submitOutput(stepId: string, stepOutput: string) {
this.submittedStepOutputs.set(stepId, stepOutput);
public submitStepRun(stepRunId: string, response: string) {
this.submittedStepOutputs.set(stepRunId, response);

if (cachedStepInputs.has(stepId) && this.isOutputValid(stepOutput)) {
const cachedInputString = cachedStepInputs.get(stepId);
cachedStepOutputs.set(cachedInputString, stepOutput);
if (cachedStepInputs.has(stepRunId) && this.isOutputValid(response)) {
const cachedInputString = cachedStepInputs.get(stepRunId);
cachedStepOutputs.set(cachedInputString, response);
}
}
}

0 comments on commit 3d86904

Please sign in to comment.