Skip to content

Commit

Permalink
fix transfer and object generation
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Feb 12, 2025
1 parent beab6b3 commit 20c7d44
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 651 deletions.
36 changes: 32 additions & 4 deletions packages/core/src/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,25 +264,53 @@ export async function generateTrueOrFalse({
export const generateObject = async ({
runtime,
context,
modelClass = ModelClass.TEXT_SMALL,
modelClass = ModelClass.TEXT_LARGE,
stopSequences,
}: GenerateObjectOptions): Promise<any> => {
if (!context) {
const errorMessage = "generateObject context is empty";
console.error(errorMessage);
throw new Error(errorMessage);
}
console.log(context);

const { object } = await runtime.useModel(modelClass, {
const obj = await runtime.useModel(modelClass, {
runtime,
context,
modelClass,
stopSequences,
object: true,
});

logger.debug(`Received Object response from ${modelClass} model.`);
return object;
console.log(obj);


let jsonString = obj;

// try to find a first and last bracket
const firstBracket = obj.indexOf("{");
const lastBracket = obj.lastIndexOf("}");
if (firstBracket !== -1 && lastBracket !== -1 && firstBracket < lastBracket) {
jsonString = obj.slice(firstBracket, lastBracket + 1);
}

console.log("jsonString", jsonString);

if (jsonString.length === 0) {
logger.error("Failed to extract JSON string from model response");
return null;
}

// parse the json string
try {
const json = JSON.parse(jsonString);
console.log("json exported", json);
return json;
} catch (error) {
logger.error("Failed to parse JSON string");
logger.error(jsonString);
return null;
}
};

export async function generateObjectArray({
Expand Down
Loading

0 comments on commit 20c7d44

Please sign in to comment.