Coverage for mall/prompt.py: 96%

52 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-15 15:57 -0500

1def sentiment(options, additional=""): 

2 new_options = process_labels( 

3 options, 

4 "Return only one of the following answers: {values} ", 

5 "- If the text is {key}, return {value} ", 

6 ) 

7 msg = [ 

8 { 

9 "role": "user", 

10 "content": "You are a helpful sentiment engine. " 

11 + f"{new_options}. " 

12 + "No capitalization. No explanations. " 

13 + f"{additional} " 

14 + "The answer is based on the following text:\n{}", 

15 } 

16 ] 

17 return msg 

18 

19 

20def summarize(max_words, additional=""): 

21 msg = [ 

22 { 

23 "role": "user", 

24 "content": "You are a helpful summarization engine. " 

25 + "Your answer will contain no no capitalization and no explanations. " 

26 + f"Return no more than " 

27 + str(max_words) 

28 + " words. " 

29 + f" {additional} " 

30 + "The answer is the summary of the following text:\n{}", 

31 } 

32 ] 

33 return msg 

34 

35 

36def translate(language, additional=""): 

37 msg = [ 

38 { 

39 "role": "user", 

40 "content": "You are a helpful translation engine. " 

41 + "You will return only the translation text, no explanations. " 

42 + f"The target language to translate to is: {language}. " 

43 + f" {additional} " 

44 + "The answer is the translation of the following text:\n{}", 

45 } 

46 ] 

47 return msg 

48 

49 

50def classify(labels, additional=""): 

51 new_labels = process_labels( 

52 labels, 

53 "Determine if the text refers to one of the following:{values} ", 

54 "- If the text is {key}, return {value} ", 

55 ) 

56 msg = [ 

57 { 

58 "role": "user", 

59 "content": "You are a helpful classification engine. " 

60 + f"{new_labels}. " 

61 + "No capitalization. No explanations. " 

62 + f"{additional} " 

63 + "The answer is based on the following text:\n{}", 

64 } 

65 ] 

66 return msg 

67 

68 

69def extract(labels, additional=""): 

70 col_labels = "" 

71 if isinstance(labels, list): 

72 no_labels = len(labels) 

73 plural = "s" 

74 text_multi = ( 

75 "Return the response exclusively in a pipe separated list, and no headers. " 

76 ) 

77 for label in labels: 

78 col_labels += label + " " 

79 col_labels = col_labels.rstrip() 

80 col_labels = col_labels.replace(" ", ", ") 

81 else: 

82 no_labels = 1 

83 plural = "" 

84 text_multi = "" 

85 col_labels = labels 

86 

87 msg = [ 

88 { 

89 "role": "user", 

90 "content": "You are a helpful text extraction engine. " 

91 + f"Extract the {col_labels} being referred to on the text. " 

92 + f"I expect {no_labels} item{plural} exactly. " 

93 + "No capitalization. No explanations. " 

94 + f" {text_multi} " 

95 + f" {additional} " 

96 + "The answer is based on the following text:\n{}", 

97 } 

98 ] 

99 return msg 

100 

101 

102def verify(what, additional=""): 

103 msg = [ 

104 { 

105 "role": "user", 

106 "content": "You are a helpful text analysis engine. " 

107 + "Determine this is true " 

108 + f"'{what}'." 

109 + "No capitalization. No explanations. " 

110 + f"{additional} " 

111 + "The answer is based on the following text:\n{}", 

112 } 

113 ] 

114 return msg 

115 

116 

117def custom(prompt): 

118 msg = [{"role": "user", "content": f"{prompt}" + ": \n{}"}] 

119 return msg 

120 

121 

122def process_labels(x, if_list="", if_dict=""): 

123 if isinstance(x, list): 

124 out = "" 

125 for i in x: 

126 out += " " + i 

127 out = out.strip() 

128 out = out.replace(" ", ", ") 

129 out = if_list.replace("{values}", str(out)) 

130 if isinstance(x, dict): 

131 out = "" 

132 for i in x: 

133 new = if_dict 

134 new = new.replace("{key}", i) 

135 new = new.replace("{value}", str(x.get(i))) 

136 out += " " + new 

137 return out