File size: 3,212 Bytes
97d35d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import ai21
import os

ai21.api_key = os.getenv("HF_KEY")


def text_completion(model, prompt, numResults, maxTokens, temperature, topKReturn, topP):
  response = ai21.Completion.execute(
    model=model,
    prompt=prompt,
    numResults=numResults,
    maxTokens=maxTokens,
    temperature=temperature,
    topKReturn=topKReturn,
    topP=topP,
    presencePenalty={
      "scale": 1,
      "applyToNumbers": True,
      "applyToPunctuations": True,
      "applyToStopwords": True,
      "applyToWhitespaces": True,
      "applyToEmojis": True
    },
    countPenalty={
      "scale": 1,
      "applyToNumbers": True,
      "applyToPunctuations": True,
      "applyToStopwords": True,
      "applyToWhitespaces": True,
      "applyToEmojis": True
    },
    frequencyPenalty={
      "scale": 1,
      "applyToNumbers": True,
      "applyToPunctuations": True,
      "applyToStopwords": True,
      "applyToWhitespaces": True,
      "applyToEmojis": True
    },
    stopSequences=[]
  )
  return response.suggestions[0].text

def chat(model, messages, numResults, maxTokens, temperature, topKReturn, topP):
  response = ai21.Chat.execute(
    model=model,
    messages=messages,
    numResults=numResults,
    maxTokens=maxTokens,
    temperature=temperature,
    topKReturn=topKReturn,
    topP=topP,
    presencePenalty={
      "scale": 1,
      "applyToNumbers": True,
      "applyToPunctuations": True,
      "applyToStopwords": True,
      "applyToWhitespaces": True,
      "applyToEmojis": True
    },
    countPenalty={
      "scale": 1,
      "applyToNumbers": True,
      "applyToPunctuations": True,
      "applyToStopwords": True,
      "applyToWhitespaces": True,
      "applyToEmojis": True
    },
    frequencyPenalty={
      "scale": 1,
      "applyToNumbers": True,
      "applyToPunctuations": True,
      "applyToStopwords": True,
      "applyToWhitespaces": True,
      "applyToEmojis": True
    },
    stopSequences=[]
  )
  return response.suggestions[0].text

def GEC(text):
  response =  ai21.GEC.execute(text=text)
  l = len(response.corrections)
  for i in range(l):
    sug = response.corrections[i].suggestion
    start = response.corrections[i].startIndex
    end = response.corrections[i].endIndex
    text = text.replace(
      text[start:end],
      sug
    )
  return text

def summarize(text):
  response = ai21.Summarize.execute(source=text, sourceType="TEXT")
  return response.summary

def improvements(text):
  response = ai21.Improvements.execute(
    text=text,
    types=[
      'fluency',
      'vocabulary/specificity',
      'vocabulary/variety',
      'clarity/short-sentences',
      'clarity/conciseness'
    ]
  )
  l = len(response.improvements)
  for i in range(l):
    sug = response.improvements[i].suggestions[0]
    start = response.improvements[i].startIndex
    end = response.improvements[i].endIndex
    text = text.replace(
      text[start:end],
      sug
    )

  return text

def paraphrase(text):
  response = ai21.Paraphrase.execute(text=text, style="general")
  return response.suggestions[0].text

def contextual_answer(context, question):
  response = ai21.Answer.execute(context=context, question=question)
  return response.suggestions[0].text