Skip to content

Commit

Permalink
enabling more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Feb 6, 2025
1 parent 9162a46 commit ca4318b
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 23 deletions.
3 changes: 2 additions & 1 deletion packages/sample/genaisrc/assistant.genai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ script({
},
})
$`Say something.`
assistant("Hello, world!")
$`hello`.role("assistant")
assistant("world!")
11 changes: 3 additions & 8 deletions packages/sample/genaisrc/dup.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
script({
choices: ["FAIL", "SUCCESS"],
tests: {
keywords: "SUCCESS",
},
})
script({})
def("FILE", "hello world")
def("FILE", "hello world")

defOutputProcessor((output) => {
const msg = output.messages.find(m => m.role === "user")
const msg = output.messages.find((m) => m.role === "user")
console.log({ msg })
const fileCount = (msg.content.match(/<FILE>/g) || []).length;
const fileCount = (msg.content.match(/<FILE>/g) || []).length
if (fileCount > 2) throw new Error("Too many files")
})
3 changes: 3 additions & 0 deletions packages/sample/genaisrc/poemfile.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
script({
tests: {}
})
console.log({ runDir: env.runDir })
$`Generate a 1 sentence poem and save it to a text file.`
defFileOutput("poem.txt", "the generated poem")
4 changes: 4 additions & 0 deletions packages/sample/genaisrc/pyodide.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
script({
model:"small",
tests: {}
})
const py = await host.python()
const version = await py.run(`import sys
sys.version`)
Expand Down
4 changes: 4 additions & 0 deletions packages/sample/genaisrc/readxl10.genai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ script({
title: "readxl10",
group: "spreadsheets",
system: ["system"],
files: "src/penguins.xlsx",
tests: {
files: "src/penguins.xlsx",
},
})

const sheets = await parsers.XLSX(env.files[0])
Expand Down
3 changes: 3 additions & 0 deletions packages/sample/genaisrc/schema_tool.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
script({
tools: ["data_infer_schema"],
files: ["src/sample.json"],
tests: {
files: ["src/sample.json"],
},
})

const file = env.files[0]
Expand Down
1 change: 0 additions & 1 deletion packages/sample/genaisrc/synthlang.genai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ MODEL: "Chinese: '山'. English summary: 'The mountain is beautiful in spring.'"
- If conflicting modifiers appear, address whichever has higher priority (p=5 > p=2).
- If no clear resolution, ask the user for clarification.`.role("system")


$`
# Blog post generation with SEO optimization
↹ topic"AI in Healthcare" @industry_context
Expand Down
4 changes: 4 additions & 0 deletions packages/sample/genaisrc/technical.genai.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ script({
group: "documentation",
system: ["system.files", "system.technical"],
temperature: 0,
files: "src/rag/markdown.md",
tests: {
files: "src/rag/markdown.md",
},
})

def("FILE", env.files)
Expand Down
2 changes: 1 addition & 1 deletion packages/sample/genaisrc/think.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
script({
model: "ollama:deepseek-r1:1.5b"
model: "ollama:deepseek-r1:1.5b",
})

$`Think about a new joke and recite it.`
1 change: 1 addition & 0 deletions packages/sample/genaisrc/transformers-all.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
script({
files: [],
tests: {},
})

import { pipeline, env as transformersEnv } from "@huggingface/transformers"
Expand Down
27 changes: 15 additions & 12 deletions packages/sample/genaisrc/transformers-rank.genai.mts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
script({
files: "src/rag/*"
files: "src/rag/*",
tests: {},
})

import { pipeline, cos_sim } from '@huggingface/transformers';
import { pipeline, cos_sim } from "@huggingface/transformers"

const featureExtractor = await pipeline('feature-extraction');
const featureExtractor = await pipeline("feature-extraction")

// Function to rank documents
async function rank(query: string, files: WorkspaceFile[]) {
const documents = files.map(({ content }) => content);
const documents = files.map(({ content }) => content)
const queryEmbedding = (await featureExtractor(query)).tolist()[0][0]
const similarities = await Promise.all(documents.map(async (doc, i) => {
const docEmbedding = (await featureExtractor(doc)).tolist()[0][0]
const score = cos_sim(queryEmbedding, docEmbedding);
return { file: files[i], score };
}));
similarities.sort((a, b) => b.score - a.score);
const similarities = await Promise.all(
documents.map(async (doc, i) => {
const docEmbedding = (await featureExtractor(doc)).tolist()[0][0]
const score = cos_sim(queryEmbedding, docEmbedding)
return { file: files[i], score }
})
)
similarities.sort((a, b) => b.score - a.score)
return similarities
}

// Example usage
const query = "What is markdown?";
const query = "What is markdown?"
const rankedDocs = await rank(query, env.files)
console.log(rankedDocs)
console.log(rankedDocs)
1 change: 1 addition & 0 deletions packages/sample/genaisrc/transformers.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
script({
model: "transformers:onnx-community/Qwen2.5-Coder-0.5B-Instruct:q4",
tests: {},
})

const res = await runPrompt(
Expand Down
3 changes: 3 additions & 0 deletions packages/sample/genaisrc/video-agent.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
script({
tools: ["agent_video"],
files: "src/audio/helloworld.mp4",
tests: {
files: "src/audio/helloworld.mp4",
},
})

const file = env.files[0]
Expand Down
3 changes: 3 additions & 0 deletions packages/sample/genaisrc/video-frames.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
script({
tools: "video_extract_frames",
files: "src/audio/helloworld.mp4",
tests: {
files: "src/audio/helloworld.mp4",
},
})
const frames = await ffmpeg.extractFrames(env.files[0], { count: 10 })
defImages(frames)
Expand Down
1 change: 1 addition & 0 deletions packages/sample/genaisrc/video.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
script({
tools: "video_extract_frames",
tests: {},
})
const info = await ffmpeg.probe("src/audio/helloworld.mp4")
console.log(JSON.stringify(info, null, 2))
Expand Down

0 comments on commit ca4318b

Please sign in to comment.