浏览代码

changed vercel prompt structure

Mish Ushakov 1 年之前
父节点
当前提交
67f04df34f
共有 3 个文件被更改,包括 14 次插入15 次删除
  1. 1 1
      examples/ollama.ts
  2. 1 1
      src/cleanup.ts
  3. 12 13
      src/models.ts

+ 1 - 1
examples/ollama.ts

@@ -23,7 +23,7 @@ const schema = z.object({
 
 // Run the scraper
 const { data } = await scraper.run(page, schema, {
-  format: 'cleanup',
+  format: 'html',
 })
 
 console.log(data)

+ 1 - 1
src/cleanup.ts

@@ -53,7 +53,7 @@ export default function cleanup() {
     }
 
     Array.from(element.attributes).forEach((attr) => {
-      if (attributesToRemove.some((a) => new RegExp(a).test(attr.name))) {
+      if (attributesToRemove.some((a) => attr.name.startsWith(a))) {
         element.removeAttribute(attr.name)
       }
     })

+ 12 - 13
src/models.ts

@@ -19,13 +19,9 @@ export type ScraperCompletionResult<T extends z.ZodSchema<any>> = {
 const defaultPrompt =
   'You are a sophisticated web scraper. Extract the contents of the webpage'
 
-function prepareAISDKPage(
-  prompt: string,
-  page: ScraperLoadResult
-): UserContent {
+function prepareAISDKPage(page: ScraperLoadResult): UserContent {
   if (page.format === 'image') {
     return [
-      { type: 'text', text: prompt },
       {
         type: 'image',
         image: page.content,
@@ -33,10 +29,7 @@ function prepareAISDKPage(
     ]
   }
 
-  return [
-    { type: 'text', text: prompt },
-    { type: 'text', text: page.content },
-  ]
+  return [{ type: 'text', text: page.content }]
 }
 
 export async function generateAISDKCompletions<T extends z.ZodSchema<any>>(
@@ -45,10 +38,13 @@ export async function generateAISDKCompletions<T extends z.ZodSchema<any>>(
   schema: T,
   options: ScraperLLMOptions
 ) {
-  const content = prepareAISDKPage(options.prompt || defaultPrompt, page)
+  const content = prepareAISDKPage(page)
   const result = await generateObject<z.infer<T>>({
     model,
-    messages: [{ role: 'user', content }],
+    messages: [
+      { role: 'system', content: options.prompt || defaultPrompt },
+      { role: 'user', content },
+    ],
     schema,
     temperature: options.temperature,
     maxTokens: options.maxTokens,
@@ -67,10 +63,13 @@ export async function streamAISDKCompletions<T extends z.ZodSchema<any>>(
   schema: T,
   options: ScraperLLMOptions
 ) {
-  const content = prepareAISDKPage(options.prompt || defaultPrompt, page)
+  const content = prepareAISDKPage(page)
   const { partialObjectStream } = await streamObject<z.infer<T>>({
     model,
-    messages: [{ role: 'user', content }],
+    messages: [
+      { role: 'system', content: options.prompt || defaultPrompt },
+      { role: 'user', content },
+    ],
     schema,
     temperature: options.temperature,
     maxTokens: options.maxTokens,