@hypermode/functions-as
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -12,3 +12,9 @@ { | ||
"plugins": ["@typescript-eslint"], | ||
"rules": {} | ||
"ignorePatterns": ["*.cjs"], | ||
"overrides": [ | ||
{ | ||
"files": ["./assembly/**/*.ts"], | ||
"parser": "./eslintParser.cjs" | ||
} | ||
] | ||
} |
@@ -18,3 +18,2 @@ import { model, ClassificationProbability, ClassificationResult } from ".."; | ||
const result = model.classifyTexts("modelId", texts); | ||
log(result); | ||
const expectedProbs1 = new ClassificationResult(); | ||
@@ -54,3 +53,3 @@ expectedProbs1.probabilities = [ | ||
const input = "sentence"; | ||
const result = model.invokeTextGenerator("modelId", "instruction", input); | ||
const result = model.generateText("modelId", "instruction", input); | ||
const expected = input; | ||
@@ -60,2 +59,28 @@ | ||
}); | ||
it("can generate an object", () => { | ||
const input = '{"input": "sentence"}'; | ||
const sample = new Map<string, string>(); | ||
const result = model.generate<Map<string, string>>( | ||
"modelId", | ||
"instruction", | ||
input, | ||
sample, | ||
); | ||
expect(result.get("input")).toBe("sentence"); | ||
}); | ||
it("can generate a list of objects", () => { | ||
const input = '{"input": "sentence"}'; | ||
const sample = new Map<string, string>(); | ||
const result = model.generateList<Map<string, string>>( | ||
"modelId", | ||
"instruction", | ||
input, | ||
sample, | ||
); | ||
expect(result.length).toBe(2); | ||
expect(result[0].get("input")).toBe("sentence"); | ||
expect(result[1].get("input")).toBe("sentence"); | ||
}); | ||
}); |
@@ -0,1 +1,4 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-nocheck: decorators are allowed on function exports in AssemblyScript | ||
// This file should only export functions from the "hypermode" host module. | ||
@@ -24,2 +27,4 @@ | ||
@external("invokeTextGenerator_v2") | ||
export declare function invokeTextGenerator( | ||
@@ -29,2 +34,3 @@ modelId: string, | ||
sentence: string, | ||
format: string, | ||
): string; |
@@ -79,12 +79,4 @@ import * as host from "./hypermode"; | ||
} | ||
public static invokeTextGenerator( | ||
modelId: string, | ||
instruction: string, | ||
text: string, | ||
): string { | ||
const response = host.invokeTextGenerator(modelId, instruction, text); | ||
static extractChatFirstMessageContent(response: string): string { | ||
const resp = JSON.parse<ChatResponse>(response); | ||
let output = ""; | ||
@@ -97,2 +89,68 @@ if (resp.choices != null) { | ||
} | ||
public static generateText( | ||
modelId: string, | ||
instruction: string, | ||
text: string, | ||
): string { | ||
const response = host.invokeTextGenerator( | ||
modelId, | ||
instruction, | ||
text, | ||
"text", | ||
); | ||
return this.extractChatFirstMessageContent(response); | ||
} | ||
public static generate<TData>( | ||
modelId: string, | ||
instruction: string, | ||
text: string, | ||
sample: TData, | ||
): TData { | ||
// Prompt trick: ask for a simple JSON object. | ||
const modifiedInstruction = | ||
"Only respond with valid JSON object in this format:\n" + | ||
JSON.stringify(sample) + | ||
"\n" + | ||
instruction; | ||
const generated = host.invokeTextGenerator( | ||
modelId, | ||
modifiedInstruction, | ||
text, | ||
"json_object", | ||
); | ||
const response = this.extractChatFirstMessageContent(generated); | ||
return JSON.parse<TData>(response, true); | ||
} | ||
public static generateList<TData>( | ||
modelId: string, | ||
instruction: string, | ||
text: string, | ||
sample: TData, | ||
): TData[] { | ||
// Prompt trick: ask for a simple JSON object containing a list. | ||
// Note, OpenAI will not generate an array of objects directly. | ||
const modifiedInstruction = | ||
"Only respond with valid JSON object containing a valid JSON array named 'list', in this format:\n" + | ||
'{"list":[' + | ||
JSON.stringify(sample) + | ||
"]}\n" + | ||
instruction; | ||
const generated = host.invokeTextGenerator( | ||
modelId, | ||
modifiedInstruction, | ||
text, | ||
"json_object", | ||
); | ||
const response = this.extractChatFirstMessageContent(generated); | ||
const jsonList = JSON.parse<Map<string, TData[]>>(response, true); | ||
return jsonList.get("list"); | ||
} | ||
} | ||
@@ -99,0 +157,0 @@ |
@@ -66,10 +66,30 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ | ||
invokeTextGenerator(pModelId, pInstruction, pSentence) { | ||
invokeTextGenerator_v2(pModelId, pInstruction, pSentence, pFormat) { | ||
const modelId = this.getString(pModelId); | ||
const instruction = this.getString(pInstruction); | ||
const sentence = this.getString(pSentence); | ||
const format = this.getString(pFormat); | ||
let response; | ||
switch (format) { | ||
case "text": | ||
response = sentence; | ||
break; | ||
case "json_object": | ||
if (instruction.includes("JSON array")) { | ||
response = `{"list":[${sentence},${sentence}]}`; | ||
} else { | ||
response = sentence; | ||
} | ||
break; | ||
default: | ||
throw new Error(`Unknown format: ${format}`); | ||
} | ||
return this.newString( | ||
'{"choices": [ {"message": {"role": "assistant", "content": ' + | ||
JSON.stringify(sentence) + | ||
'{"choices":[{"message":{"role":"assistant","content":' + | ||
JSON.stringify(response) + | ||
"}}]}", | ||
@@ -85,3 +105,3 @@ ); | ||
computeEmbedding: this.computeEmbedding.bind(this), | ||
invokeTextGenerator: this.invokeTextGenerator.bind(this), | ||
invokeTextGenerator_v2: this.invokeTextGenerator_v2.bind(this), | ||
}; | ||
@@ -88,0 +108,0 @@ } |
{ | ||
"name": "@hypermode/functions-as", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Hypermode library for AssemblyScript functions", | ||
@@ -21,5 +21,5 @@ "author": "Hypermode, Inc.", | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^7.1.1", | ||
"@typescript-eslint/parser": "^7.1.1", | ||
"assemblyscript": "^0.27.24", | ||
"@typescript-eslint/eslint-plugin": "^7.3.1", | ||
"@typescript-eslint/parser": "^7.3.1", | ||
"assemblyscript": "^0.27.25", | ||
"assemblyscript-prettier": "^3.0.1", | ||
@@ -26,0 +26,0 @@ "eslint": "^8.57.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20525
20
637