extractwp.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { Ollama } from "ollama-node";
  2. import { readFile } from "fs/promises";
  3. async function main() {
  4. const ollama = new Ollama();
  5. // Set the system prompt to prepare the model to receive a prompt and a schema and set some rules for the output.
  6. const systemprompt = `You will be given a text along with a prompt and a schema. You will have to extract the information requested in the prompt from the text and generate output in JSON observing the schema provided. If the schema shows a type of integer or number, you must only show a integer for that field. A string should always be a valid string. If a value is unknown, leave it empty. Output the JSON with extra spaces to ensure that it pretty prints.`
  7. const schema = {
  8. "people": [{
  9. "name": {
  10. "type": "string",
  11. "description": "Name of the person"
  12. },
  13. "title": {
  14. "type": "string",
  15. "description": "Title of the person"
  16. }
  17. }],
  18. }
  19. // Depending on the model chosen, you may be limited by the size of the context window, so limit the context to 2000 words.
  20. const textcontent = await readFile("./wp.txt", "utf-8").then((text) => text.split(" ").slice(0, 2000).join(" "));
  21. // Specific instructions for this task
  22. const prompt = `Review the source text and determine the 10 most important people to focus on. Then extract the name and title for those people. Output should be in JSON.\n\nSchema: \n${JSON.stringify(schema, null, 2)}\n\nSource Text:\n${textcontent}`
  23. await ollama.setModel("neural-chat");
  24. ollama.setSystemPrompt(systemprompt);
  25. // setJSONFormat is the equivalent of setting 'format: json' in the API
  26. ollama.setJSONFormat(true);
  27. await ollama.streamingGenerate(prompt, (word) => { process.stdout.write(word) })
  28. }
  29. main();