Skip to content

Commit

Permalink
Merge pull request #50 from dcSpark/nico/update_download_pages
Browse files Browse the repository at this point in the history
update download page tool
  • Loading branch information
nicarq authored Sep 28, 2024
2 parents 7ceebb0 + 7853103 commit 8a92ffc
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 29 deletions.
18 changes: 18 additions & 0 deletions apps/shinkai-tool-download-page/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,21 @@ test('exists definition', () => {
const definition = tool.getDefinition();
expect(definition).toBeInstanceOf(Object);
});

// Note: Uncomment for running locally
// test('can download and convert two pages to Markdown', async () => {
// const tool = new Tool({});
// const params = {
// urls: [
// 'https://en.wikipedia.org/wiki/MTOR',
// 'https://en.wikipedia.org/wiki/Xkcd'
// ]
// };

// const result = await tool.run(params);

// expect(result.data.markdowns).toHaveLength(2);
// result.data.markdowns.forEach(markdown => {
// expect(markdown).toContain('#'); // Basic check to see if Markdown content is present
// });
// });
37 changes: 19 additions & 18 deletions apps/shinkai-tool-download-page/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { BaseTool, RunResult } from '@shinkai_protocol/shinkai-tools-builder';
import { ToolDefinition } from 'libs/shinkai-tools-builder/src/tool-definition';
import TurndownService from 'turndown';
import TurndownService = require('turndown');
import axios from 'axios';

type Config = {};
type Params = {
url: string;
urls: string[];
};

type Result = { markdown: string };

type Result = { markdowns: string[] };

export class Tool extends BaseTool<Config, Params, Result> {
definition: ToolDefinition<Config, Params, Result> = {
id: 'shinkai-tool-download-page',
name: 'Shinkai: Download Page',
description: 'Downloads a URL and converts its HTML content to Markdown',
id: 'shinkai-tool-download-pages',
name: 'Shinkai: Download Pages',
description: 'Downloads one or more URLs and converts their HTML content to Markdown',
author: 'Shinkai',
keywords: ['download page', 'url to markdown', 'shinkai'],
keywords: ['HTML to Markdown', 'web page downloader', 'content conversion', 'URL to Markdown'],
configurations: {
type: 'object',
properties: {},
Expand All @@ -26,25 +25,27 @@ export class Tool extends BaseTool<Config, Params, Result> {
parameters: {
type: 'object',
properties: {
url: { type: 'string' },
urls: { type: 'array', items: { type: 'string' } },
},
required: ['url'],
required: ['urls'],
},
result: {
type: 'object',
properties: {
markdown: { type: 'string' },
markdowns: { type: 'array', items: { type: 'string' } },
},
required: ['markdown'],
required: ['markdowns'],
},
};

async run(params: Params): Promise<RunResult<Result>> {
await process.nextTick(() => { });
const response = await axios.get(params.url);
const html = response.data;
const turndownService = new TurndownService();
const markdown = turndownService.turndown(html);
return Promise.resolve({ data: { markdown } });
try {
const responses = await axios.all(params.urls.map(url => axios.get(url)));
const turndownService = new TurndownService();
const markdowns = responses.map(response => turndownService.turndown(response.data));
return Promise.resolve({ data: { markdowns } });
} catch (error) {
return Promise.resolve({ data: { markdowns: [] } });
}
}
}
15 changes: 15 additions & 0 deletions apps/shinkai-tool-json-to-md/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,18 @@ test('Tool converts JSON to Markdown correctly', async () => {
expect(result.data.message.trim()).toBe(expectedMarkdown.trim());
});


test('Tool converts new JSON to Markdown correctly', async () => {
const tool = new Tool({
only_system: false,
});

const newParams = {
message: '{\n \"relevantSentencesFromText\": [\n {\n \"citation_id\": 5,\n \"document_reference\": \"[8] http://www.youtube.com/watch?v=eaSIq9c14YE\",\n \"relevantSentenceFromDocument\": \"This video describes the role of light in plant growth. A comparison of light detection by human eyes and light absorption by plants begins a little bit past the halfway point.\"\n },\n {\n \"citation_id\": 0,\n \"document_reference\": \"[6] Arizona Master Gardener Manual, by the University of Arizona College of Agriculture’s Cooperative Extension.\",\n \"relevantSentenceFromDocument\": \"This online book is a good example of a state-specific resource for learning about what plants need to thrive.\"\n },\n {\n \"citation_id\": 0,\n \"document_reference\": \"[6] Arizona Master Gardener Manual, by the University of Arizona College of Agriculture’s Cooperative Extension.\",\n \"relevantSentenceFromDocument\": \"This online book is a good example of a state-specific resource for learning about what plants need to thrive.\"\n },\n {\n \"citation_id\": 11,\n \"document_reference\": \"[7]\",\n \"relevantSentenceFromDocument\": \"\"\n }\n ],\n \"answer\": {\n \"brief_introduction\": {\n \"sentences\": [\n \"Transplanting a houseplant can be a delicate process that requires careful consideration of the plant’s needs and the conditions provided.\",\n \"To ensure a successful transplant, it is essential to prepare the new pot and the soil, as well as the plant itself, in advance.\",\n \"As explained by [0] on the Arizona Master Gardener Manual website,[6] this preparation can help reduce stress on the plant during the transplant process.\"\n ]\n },\n \"extensive_body\": [\n {\n \"sentences\": [\n \"Firstly, as [1] describes, plants need light to grow and thrive. But different colors of light have varying effects on their development. For example, scientists on UCSB ScienceLine state that \\\"red light is often used to boost fruit production in greenhouses, while blue light can be used to increase plant growth\\\" [3].\",\n \"When transplanting a houseplant, it is essential to choose a location with the right amount of light. As [1] explains, \\\"a comparison of light detection by human eyes and light absorption by plants begins a little bit past the halfway point.\\\" This means that even if a plant appears healthy in its current environment, it may require more or less light once moved to a new pot.\",\n \"In addition to ensuring the right amount of light, it is crucial to prepare the soil properly. According to [10] on Arizona Master Gardener Manual website,[6]\",\n\n \"Furthermore, plants have unique needs and preferences for optimal growth. As [1] notes that \\\"Plants are able to sense changes in their environment using abilities similar to human sight, touch, smell, taste, and hearing.\\\" This means that even small changes can affect the plant’s overall well-being.\",\n \"To minimize transplant shock, gardeners should also consider the time of year when making a decision to move or transplant any plants. According to [4], it is best to do this during the spring season because this allows most species of deciduous trees and many other woody ornamental woody shrubs including fruit trees to start regrowth without major changes in weather as they usually require.\",\n \"In terms of optimal growth conditions, different types of plants have varying requirements when it comes to light. [2] states that \\\"by using specialized colored filters over light lamps can produce higher plant weights\\\" which demonstrates the fact various colors used for plants growth at high concentration affect its height length and plant biomass. Various experiments conducted, by some research studies demonstrated 1-2-fold increase of both biomass growth rate as well plant quality in comparison with an equivalent plant grown without such lighting filters.\"\n ]\n }\n ],\n \"conclusion\": [\n {\n \"sentences\": [\n \"In conclusion, transplanting a houseplant requires careful consideration of its needs and the new environment. With this information from state-specific resources like Arizona Master Gardener Manual,[6] gardeners are able to make more informed decisions about optimal growing conditions.\",\n \"Moreover, plants respond well-t their environment; as mentioned by Abram, \\\"Growing Plants from Seed\\\"[14] which further highlights various factors that should be considered when transplanting a plant.\"\n ]\n }\n ],\n },\n}',
template: '# Introduction\\n{%- for sentence in answer.brief_introduction.sentences %}\\n{{ sentence }}\\n{%- endfor %}\\n\\n# Body\\n{%- for section in answer.extensive_body %}\\n## Section {{ loop.index }}\\n{%- for sentence in section.sentences %}\\n{{ sentence }}\\n{%- endfor %}\\n{%- endfor %}\\n\\n# Conclusion\\n{%- for section in answer.conclusion %}\\n{{ section.sentences[0] }}\\n{%- endfor %}\\n\\n# Citations\\n{%- for citation in relevantSentencesFromText %}\\n[{{ citation.citation_id }}]: {{ citation.relevantSentenceFromDocument }}\\n{%- endfor %}',
};

const newResult = await tool.run(newParams);
expect(newResult).toHaveProperty('data.message');
expect(newResult.data.message.trim()).toBe('');
});
16 changes: 8 additions & 8 deletions apps/shinkai-tool-youtube-summary/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ test('exists definition', async () => {
expect(definition).toBeInstanceOf(Object);
});

test('transcript video', async () => {
const tool = new Tool({});
const result = await tool.run({
url: 'https://www.youtube.com/watch?v=SUj34OWkjXU',
});
expect(result.data.summary.length).toBeGreaterThan(0);
console.log(result.data.summary);
}, 30000);
// test('transcript video', async () => {
// const tool = new Tool({});
// const result = await tool.run({
// url: 'https://www.youtube.com/watch?v=SUj34OWkjXU',
// });
// expect(result.data.summary.length).toBeGreaterThan(0);
// console.log(result.data.summary);
// }, 30000);


// test('transcript video using openai', async () => {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shinkai_protocol/source",
"version": "0.7.15",
"version": "0.7.16",
"description": "This repository serves as the ecosystem to execute Shinkai tools, provided by the Shinkai team or third-party developers, in a secure environment. It provides a sandboxed space for executing these tools, ensuring that they run safely and efficiently, while also allowing for seamless integration with Rust code.",
"main": "index.js",
"author": "",
Expand Down
1 change: 1 addition & 0 deletions scripts/tool-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ build({
platform: 'node',
target: 'node20.16',
outfile: outputFile,
loader: { '.node': 'file' },
})
.then(async () => {
const code = await fs.promises.readFile(outputFile, 'utf-8');
Expand Down

0 comments on commit 8a92ffc

Please sign in to comment.