Skip to content

Commit

Permalink
feat: use AI to parse content
Browse files Browse the repository at this point in the history
  • Loading branch information
telesoho committed Sep 17, 2024
1 parent 4af38b6 commit 3077128
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 70 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,22 @@
},
"default": [
{
"model": "llama-3.1-8b-instant",
"model": "llama-3.1-70b-versatile",
"messages": [
{
"role": "system",
"content": [
"You are responsible for converting text content into Markdown format. ",
"If the original content is HTML, ignore any color or font settings and comments, but retain tables.",
"Only output convered content"
"You are a helpful assistant."
]
},
{
"role": "user",
"content": "{{clipboard_text}}"
"content": [
"Generate consolation for the following text:",
"{{clipboard_text}}"
]
}
],
"tools": [],
"tool_choice": "auto",
"max_tokens": 4096
}
],
Expand Down Expand Up @@ -364,6 +363,7 @@
"arch": "^2.2.0",
"axios": "^1.7.7",
"moment": "^2.22.1",
"node-html-parser": "^6.1.13",
"openai": "^4.61.0",
"shelljs": "^0.8.5",
"turndown": "^7.1.2",
Expand Down
2 changes: 1 addition & 1 deletion src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class Logger {
if (this.channel) {
const time = moment().format("MM-DD HH:mm:ss");
for (const m of message) {
const logmsg = `[${time}] ${m}`;
const logmsg = `[${time}] ${m.substring(0, 256)}`;
this.channel.appendLine(logmsg);
}
}
Expand Down
53 changes: 25 additions & 28 deletions src/ToolsManager.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { ChatCompletionTool } from "openai/resources/chat/completions";
import { fetchWeb, htmlToMarkdown } from "./tool_functions";
import Logger from "./Logger";
import { fetchWeb } from "./tool_functions";

type FunctionParameters = {
[x: string]: unknown;
};
type ToolFunction = (...args: any[]) => any;

interface ToolInfo {
func: ToolFunction;
description: string;
parameters: Record<string, unknown>;
parameters: FunctionParameters;
}

export class ToolsManager {
Expand All @@ -18,38 +21,32 @@ export class ToolsManager {
}

public registerDefaultTools() {
this.registerTool(
"get_current_weather",
async ({ city }: { city: string }) => {
return JSON.stringify({
city: city,
temperature: "25°C",
weather: "sunny",
});
},
"Get the current weather for a specified city",
{
type: "object",
properties: {
city: { type: "string", description: "The name of the city" },
},
required: ["city"],
}
);
this.registerTool("fetchWeb", fetchWeb, "fetch a web page content", {
type: "object",
properties: {
url: { type: "string", description: "The url of the web page" },
},
required: ["url"],
});
// this.registerTool("fetchWeb", fetchWeb, "fetch a web page content", {
// type: "object",
// properties: {
// url: { type: "string", description: "The url of the web page" },
// },
// required: ["url"],
// });
// this.registerTool(
// "htmlToMarkdown",
// htmlToMarkdown,
// "Conver html to markdown",
// {
// type: "object",
// properties: {
// html: { type: "string", description: "html" },
// },
// required: ["html"],
// }
// );
}

public registerTool(
name: string,
func: ToolFunction,
description: string,
parameters: Record<string, unknown>
parameters: FunctionParameters
) {
this.tools.set(name, { func, description, parameters });
}
Expand Down
6 changes: 5 additions & 1 deletion src/ai_paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ export class AIPaster {
}`
);
});
completion.tools.forEach((tool) => {
Logger.log("tool:", JSON.stringify(tool));
});
const chatCompletion = await this.client.chat.completions.create(
completion
);
const responseMessages = chatCompletion.choices[0].message;
const toolCalls = chatCompletion.choices[0].message.tool_calls;
if (toolCalls) {
completion.messages.push(responseMessages);
for (const toolCall of toolCalls) {
const functionName = toolCall.function.name;
const functionResponse = await this.toolsManager.executeTool(
Expand Down Expand Up @@ -78,7 +82,7 @@ export class AIPaster {
}
return responseMessages.content;
} catch (error) {
Logger.log("Error", error);
Logger.log("Error:", JSON.stringify(error));
throw error;
}
}
Expand Down
43 changes: 14 additions & 29 deletions src/paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ class Paster {
}
}

static async parseByAI(content: string): Promise<Boolean> {
if (Paster.getConfig().enableAI) {
static async parseByAI(content: string) {
if (Paster.config.enableAI) {
const p = new AIPaster();
const result = await p.callAI(content);
if (result.status == "success") {
Paster.writeToEditor(result.message);
return true;
await Paster.writeToEditor(result.message);
}
}
return false;
Paster.writeToEditor(content);
}

static async selectClipboardType(
Expand All @@ -54,12 +53,6 @@ class Paster {
if (!(type instanceof Set)) {
return type;
}
if (this.config.autoSelectClipboardType == "always") {
const priorityOrdering = this.config.autoSelectClipboardTypePriority;
for (const theType of priorityOrdering)
if (type.has(theType)) return theType;
return xclip.ClipboardType.Unknown;
}
if (
this.config.autoSelectClipboardType == "never" ||
(this.config.autoSelectClipboardType == "html&text" &&
Expand All @@ -77,6 +70,10 @@ class Paster {
}
return xclip.ClipboardType.Unknown;
}
const priorityOrdering = this.config.autoSelectClipboardTypePriority;
for (const theType of priorityOrdering)
if (type.has(theType)) return theType;
return xclip.ClipboardType.Unknown;
}

/**
Expand All @@ -96,36 +93,24 @@ class Paster {
case xclip.ClipboardType.Html:
if (enableHtmlConverter) {
const html = await cb.getTextHtml();
if (await Paster.parseByAI(html)) {
return;
}
Logger.log(html);
const markdown = toMarkdown(html, turndownOptions);
let markdown = toMarkdown(html, turndownOptions);
if (enableRulesForHtml) {
let newMarkdown = Paster.parse(markdown);
Paster.writeToEditor(newMarkdown);
} else {
Paster.writeToEditor(markdown);
markdown = Paster.parse(markdown);
}
await Paster.parseByAI(markdown);
} else {
const text = await cb.getTextPlain();
if (Paster.parseByAI(text)) {
return;
}
if (text) {
let newContent = Paster.parse(text);
Paster.writeToEditor(newContent);
await Paster.parseByAI(newContent);
}
}
break;
case xclip.ClipboardType.Text:
const text = await cb.getTextPlain();
if (await Paster.parseByAI(text)) {
return;
}
if (text) {
let newContent = Paster.parse(text);
Paster.writeToEditor(newContent);
await Paster.parseByAI(newContent);
}
break;
case xclip.ClipboardType.Image:
Expand Down Expand Up @@ -450,7 +435,7 @@ class Paster {
}
}

private static parse(content) {
static parse(content) {
let editor = vscode.window.activeTextEditor;
let fileUri = editor.document.uri;

Expand Down
7 changes: 6 additions & 1 deletion src/toMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ function toMarkdown(content, options) {
// return "\n\n" + content + "\n" + underline + "\n\n";
// },
// },

{
filter: ["style", "script", "head", "meta"],
replacement: function (content) {
return "";
},
},
{
filter: "sup",
replacement: function (content) {
Expand Down
18 changes: 15 additions & 3 deletions src/tool_functions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import axios from "axios";
import { toMarkdown } from "./toMarkdown";
import { Paster } from "./paster";
import Logger from "./Logger";
import * as HTMLParser from "node-html-parser";

export async function fetchWeb({ url }: { url: string }): Promise<any> {
let content = "";
try {
const response = await axios.get(url);
content = response.data;
return { content };
let html = HTMLParser.parse(response.data);
const body = html.querySelector("body").toString();
const title = html.querySelector("title").toString();
return { url, title, body };
} catch (e) {
return {
error: e,
};
}
}

export async function htmlToMarkdown({ html }: { html: string }): Promise<any> {
Logger.log("htmlToMarkdown:", JSON.stringify(html));
let turndownOptions = Paster.config.turndownOptions;
let content = toMarkdown(html, turndownOptions);
return { content };
}

0 comments on commit 3077128

Please sign in to comment.