Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

feat: add phi3.5 auto tagging pipe

Louis Beaumontda8449b9e8db
diff --git a/examples/typescript/pipe-tagging-activity/README.md b/examples/typescript/pipe-tagging-activity/README.md
new file mode 100644
index 000000000..04948de47
--- /dev/null
+++ b/examples/typescript/pipe-tagging-activity/README.md
@@ -0,0 +1,16 @@
+
+
+usage (will run 24/7 on your computer and tag your data)
+
+ai will annotate your data based on your activity
+
+then when you search it will return results with the added ai tags
+
+```
+ollama run phi3.5
+cargo build --release --features pipes
+./target/release/screenpipe --pipe examples/typescript/pipe-tagging-activity/main.js
+```
+
+
+
diff --git a/examples/typescript/pipe-tagging-activity/main.js b/examples/typescript/pipe-tagging-activity/main.js
new file mode 100644
index 000000000..d97b86677
--- /dev/null
+++ b/examples/typescript/pipe-tagging-activity/main.js
@@ -0,0 +1,123 @@
+const INTERVAL = 1 * 60 * 1000; // 1 minute in milliseconds
+
+async function queryScreenpipe(params) {
+ try {
+ const queryParams = Object.entries(params)
+ .filter(([_, v]) => v != null)
+ .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
+ .join('&');
+ console.log("Calling Screenpipe:", JSON.stringify(params));
+ const result = await pipe.get(`http://localhost:3030/search?${queryParams}`);
+ console.log("Retrieved", result.data.length, "items from Screenpipe");
+ return result;
+ } catch (error) {
+ console.error("Error querying Screenpipe:", error);
+ return null;
+ }
+}
+
+async function getAIProvider() {
+ const provider = "ollama";
+ const model = "phi3.5";
+ return { provider, model };
+}
+
+async function generateTags(provider, screenData) {
+ const prompt = `Based on the following screen data, generate relevant tags for the user's activity:
+
+ ${JSON.stringify(screenData)}
+
+ Return a JSON object with the following structure:
+ {
+ "tags": ["tag1", "tag2", "tag3"]
+ }
+ Do not say anything else but JSON.
+ Provide 3-5 relevant tags that describe the user's activity.`;
+
+ const result = await pipe.post(
+ "http://localhost:11434/api/chat",
+ JSON.stringify({
+ model: provider.model,
+ messages: [{ role: "user", content: prompt }],
+ response_format: { type: "json_object" },
+ stream: false,
+ }),
+ );
+
+ console.log("AI answer:", result);
+
+ const content = result.message.content;
+ return JSON.parse(content);
+}
+
+async function addTags(contentType, id, tags) {
+ try {
+ console.log(`Adding tags to ${contentType} item ${id}:`, tags);
+ const result = await pipe.post(
+ `http://localhost:3030/tags/${contentType}/${id}`,
+ JSON.stringify({ tags })
+ );
+ console.log("Tags added successfully:", result);
+ return result;
+ } catch (error) {
+ console.error(`Error adding tags to ${contentType} item ${id}:`, error);
+ return null;
+ }
+}
+
+async function tagRecentActivities(provider) {
+ const now = new Date();
+ const oneMinuteAgo = new Date(now.getTime() - INTERVAL);
+
+ const screenData = await queryScreenpipe({
+ start_time: oneMinuteAgo.toISOString(),
+ end_time: now.toISOString(),
+ limit: 50,
+ content_type: "ocr",
+ });
+
+ if (!screenData || !screenData.data || screenData.data.length === 0) {
+ console.log("No data retrieved from Screenpipe");
+ return;
+ }
+
+ const { tags } = await generateTags(provider, screenData);
+
+ for (const item of screenData.data) {
+ const contentType = "vision";
+ const id = item.content.frame_id;
+ await addTags(contentType, id, tags);
+ }
+
+ console.log("Tags added to recent activities:", tags);
+}
+
+async function runActivityTagger() {
+ const provider = await getAIProvider();
+ console.log("Starting Activity Tagger with provider:", provider);
+
+ while (true) {
+ try {
+ await tagRecentActivities(provider);
+ } catch (error) {
+ console.error("Error in activity tagging:", error);
+ }
+ await new Promise(resolve => setTimeout(resolve, INTERVAL));
+ }
+}
+
+// Self-invoking async function to run the activity tagger
+(async () => {
+ try {
+ await runActivityTagger();
+ } catch (error) {
+ console.error("Fatal error in Activity Tagger:");
+ if (error instanceof Error) {
+ console.error("Error name:", error.name);
+ console.error("Error message:", error.message);
+ console.error("Error stack:", error.stack);
+ } else {
+ console.error("Unexpected error:", error);
+ }
+ }
+})();
\ No newline at end of file
diff --git a/screenpipe-core/src/pipes.rs b/screenpipe-core/src/pipes.rs
index b66b96077..9d28895b3 100644
--- a/screenpipe-core/src/pipes.rs
+++ b/screenpipe-core/src/pipes.rs
@@ -9,6 +9,9 @@ mod pipes {
use deno_core::ModuleLoadResponse;
use deno_core::ModuleSourceCode;
use log::error;
+ use reqwest::header::HeaderMap;
+ use reqwest::header::HeaderValue;
+ use reqwest::header::CONTENT_TYPE;
use std::env;
use std::rc::Rc;
@@ -58,7 +61,13 @@ mod pipes {
#[string] body: String,
) -> Result<String, AnyError> {
let client = reqwest::Client::new();
- let response = client.post(&url).body(body).send().await?;
+
+ // Create a HeaderMap and add the Content-Type header
+ let mut headers = HeaderMap::new();
+ headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
+
+ let response = client.post(&url).headers(headers).body(body).send().await?;
+
let status = response.status();
let text = response.text().await?;