-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,69 @@ | ||
import { callML } from "@/src/utils/ml" | ||
import { Run } from "shared" | ||
import { lastMsg } from "../checks" | ||
import openai from "@/src/utils/openai" | ||
import lunary from "lunary" | ||
|
||
export async function evaluate(run: Run) { | ||
const input = lastMsg(run.input) | ||
// TOOD: refacto this with all the other parsing function already in use | ||
function parseMessages(messages: unknown) { | ||
if (!messages) { | ||
return [""] | ||
} | ||
if (typeof messages === "string" && messages.length) { | ||
return [messages] | ||
} | ||
|
||
if (messages === "__NOT_INGESTED__") { | ||
return [""] | ||
} | ||
|
||
if (Array.isArray(messages)) { | ||
let contentArray = [] | ||
for (const message of messages) { | ||
let content = message.content || message.text | ||
if (typeof content === "string" && content.length) { | ||
contentArray.push(content) | ||
} else { | ||
contentArray.push(JSON.stringify(message)) | ||
} | ||
} | ||
return contentArray | ||
} | ||
|
||
if (typeof messages === "object") { | ||
return [JSON.stringify(messages)] | ||
} | ||
|
||
const template = await lunary.renderTemplate("sentiment", { | ||
input, | ||
}) | ||
return [""] | ||
} | ||
|
||
export async function evaluate(run: Run) { | ||
console.log("SENTIMENT") | ||
const input = parseMessages(run.input) | ||
const output = parseMessages(run.output) | ||
const error = parseMessages(run.error) | ||
|
||
const res = await openai.chat.completions.create(template) | ||
const [inputSentiment, outputSentiment] = await Promise.all([ | ||
analyzeSentiment(input), | ||
analyzeSentiment(output), | ||
]) | ||
|
||
const output = res.choices[0]?.message?.content | ||
const sentiments = { | ||
input: inputSentiment, | ||
output: outputSentiment, | ||
error: error.map(e => 0) | ||
} | ||
|
||
if (!output) "" | ||
|
||
const result = parseFloat(output.toLowerCase().trim()) | ||
// TODO: zod for languages, SHOLUD NOT INGEST IN DB IF NOT CORRECT FORMAT | ||
return sentiments | ||
} | ||
|
||
return result | ||
// TODO: type | ||
async function analyzeSentiment( | ||
texts: string[], | ||
): Promise<any> { | ||
try { | ||
return callML("sentiment", { texts }) | ||
} catch (error) { | ||
console.error(error) | ||
console.log(texts) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters