File size: 2,589 Bytes
e90c5a9
 
 
5382ee4
e90c5a9
 
5382ee4
e90c5a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { HfInference } from "@huggingface/inference"

import { createZephyrPrompt } from "./createZephyrPrompt.mts"
import { hfInferenceApiModel, hfApiKey } from "./config.mts"


const hf = new HfInference(hfApiKey)

export async function summarizeWithZephyr({
  news,
  neverThrow
}: {
  news: string
  neverThrow?: boolean
}): Promise<string> {
  try {
    const inputs = createZephyrPrompt([
      {
        role: "system",
        content: `Your must summarize the content into 2 or 3 sentence. DO NOT write more than than. Keep it dense and simple, and short.`,
      },
      {
        role: "user",
        content: news,
      }
    ]) //+ "\n["

    const nbMaxNewTokens = 250

    let rawBufferString = ""
    try {
      for await (const output of hf.textGenerationStream({
        model: hfInferenceApiModel,
        inputs,
        parameters: {
          do_sample: true,
          max_new_tokens: nbMaxNewTokens,
          return_full_text: false,
        }
      })) {
        rawBufferString += output.token.text
        // process.stdout.write(output.token.text)
        if (
          rawBufferString.includes("</s>") || 
          rawBufferString.includes("<s>") ||
          rawBufferString.includes("/s>") ||
          rawBufferString.includes("[INST]") ||
          rawBufferString.includes("[/INST]") ||
          rawBufferString.includes("<SYS>") ||
          rawBufferString.includes("<<SYS>>") ||
          rawBufferString.includes("</SYS>") ||
          rawBufferString.includes("<</SYS>>") ||
          rawBufferString.includes("<|user|>") ||
          rawBufferString.includes("<|end|>") ||
          rawBufferString.includes("<|system|>") ||
          rawBufferString.includes("<|assistant|>")
        ) {
          break
        }
      }
    } catch (err) {
      // console.error(`error during generation: ${err}`)

      if (`${err}` === "Error: Model is overloaded") {
        rawBufferString = ``
      }
    }

    const tmpResult = 
      rawBufferString.replaceAll("</s>", "") 
      .replaceAll("<s>", "")
      .replaceAll("/s>", "")
      .replaceAll("[INST]", "")
      .replaceAll("[/INST]", "")
      .replaceAll("<SYS>", "")
      .replaceAll("<<SYS>>", "")
      .replaceAll("</SYS>", "")
      .replaceAll("<</SYS>>", "")
      .replaceAll("<|user|>", "")
      .replaceAll("<|end|>", "")
      .replaceAll("<|system|>", "")
      .replaceAll("<|assistant|>", "")
    
  
    return tmpResult
  } catch (err) {
    if (neverThrow) {
      console.error(`summarizeWithZephyr():`, err)
      return ""
    } else {
      throw err
    }
  }
}