From cdac05b403c2fd05c4a223a8cac47c81686bef5e Mon Sep 17 00:00:00 2001 From: Jared Rhizor Date: Tue, 17 Oct 2023 19:44:57 -0700 Subject: [PATCH] improve cohere input detection (#8) * improve cohere input detection * bump version * remove cohere invalid format test, since cohere is so aggressive at matching the output format even when there are no matching instructions --- package.json | 2 +- src/elelem.test.ts | 26 -------------------------- src/elelem.ts | 5 ++++- src/formatters.ts | 2 +- 4 files changed, 6 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 819a809..514be1c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "elelem", - "version": "0.0.4", + "version": "0.0.5", "description": "Simple, opinionated, JSON-typed, and traced LLM framework for TypeScript.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/elelem.test.ts b/src/elelem.test.ts index 4aa516d..be06714 100644 --- a/src/elelem.test.ts +++ b/src/elelem.test.ts @@ -302,32 +302,6 @@ describe("cohere", () => { expect(result.foundingYear).toBe("1800"); expect(result.populationEstimate).toBeGreaterThan(500000); }, 10000); - - test("invalid format", async () => { - const wrapper = async () => { - return await llm.session( - "invalid-format", - { cohere: { model: "command" } }, - async (c) => { - const { result: cityDescription } = await c.cohere( - "city-description", - { - max_tokens: 100, - temperature: 0, - }, - `Request ${Math.random()}\n`, - "Washington, D.C.", - capitolResponseSchema, - LangchainJsonSchemaFormatter, - ); - return cityDescription; - }, - ); - }; - - // langchain formatter is worse than the one that includes examples, so this should fail with the same prompt - await expect(wrapper()).rejects.toThrowError(ElelemError); - }, 10000); }); interface AddContext { diff --git a/src/elelem.ts b/src/elelem.ts index 6afa18f..7234efc 100644 --- a/src/elelem.ts +++ b/src/elelem.ts @@ -434,11 +434,14 @@ export const elelem: Elelem = { ...modelOptions, }; + // since cohere doesn't have actual system prompts, it has trouble identifying where the input starts + const prefixedUserPrompt = `\nInput: ${userPrompt}`; + return await generate( chatId, combinedOptions, systemPrompt, - userPrompt, + prefixedUserPrompt, schema, formatter, backoffOptions, diff --git a/src/formatters.ts b/src/formatters.ts index 026331f..72d7f82 100644 --- a/src/formatters.ts +++ b/src/formatters.ts @@ -21,7 +21,7 @@ ${JSON.stringify(zodToJsonSchema(schema))} `; }; -export const akerExampleFormatter: ElelemFormatter = ( +export const FakerExampleFormatter: ElelemFormatter = ( schema: ZodType, ) => { return `