DSXiangLi commited on
Commit
29be613
1 Parent(s): 110a394
Files changed (1) hide show
  1. ape/llm.py +13 -6
ape/llm.py CHANGED
@@ -19,6 +19,8 @@ Cost = {
19
 
20
 
21
  class LLMGPT(object):
 
 
22
  def __init__(self, openai_key):
23
  self.gen_llm = ChatOpenAI(openai_api_key=openai_key, max_tokens=2000, temperature=0.7, verbose=True)
24
  self.eval_llm = OpenAI(openai_api_key=openai_key, max_tokens=2000, temperature=0.7, logprobs=1, verbose=True)
@@ -32,8 +34,8 @@ class LLMGPT(object):
32
  cost = 0.02
33
  else:
34
  cost = 0.0004
35
- encoding = tiktoken.get_encoding("cl100k_base")
36
- num_tokens = len(encoding.encode(text))
37
  total_price = ((num_tokens / 1000) * cost)
38
  return total_price
39
 
@@ -78,12 +80,17 @@ class LLMGPT(object):
78
  eval_prompt = MyTemplate['eval_prompt']
79
  prompt = PromptTemplate.from_template(eval_prompt)
80
  eval_chain = LLMChain(llm=self.eval_llm, prompt=prompt)
81
-
82
  for sample in eval_set:
 
 
83
  llmresult = eval_chain.generate([{'instruction': instruction, 'input': sample[0], 'output': sample[1]}])
84
  print(llmresult)
85
- logprobs = llmresult.generations.[0][0].generation_info['token_logprobs']
86
  print(logprobs)
87
- tokens = llmresult.generations.[0][0].generation_info['tokens']
 
88
  print(tokens)
89
- return sum(logprobs)
 
 
 
19
 
20
 
21
  class LLMGPT(object):
22
+ encoding = tiktoken.get_encoding("cl100k_base")
23
+
24
  def __init__(self, openai_key):
25
  self.gen_llm = ChatOpenAI(openai_api_key=openai_key, max_tokens=2000, temperature=0.7, verbose=True)
26
  self.eval_llm = OpenAI(openai_api_key=openai_key, max_tokens=2000, temperature=0.7, logprobs=1, verbose=True)
 
34
  cost = 0.02
35
  else:
36
  cost = 0.0004
37
+
38
+ num_tokens = len(LLMGPT.encoding.encode(text))
39
  total_price = ((num_tokens / 1000) * cost)
40
  return total_price
41
 
 
80
  eval_prompt = MyTemplate['eval_prompt']
81
  prompt = PromptTemplate.from_template(eval_prompt)
82
  eval_chain = LLMChain(llm=self.eval_llm, prompt=prompt)
83
+ score = 0
84
  for sample in eval_set:
85
+ output_len = len(LLMGPT.encoding.encode(sample[1]))
86
+ print(output_len)
87
  llmresult = eval_chain.generate([{'instruction': instruction, 'input': sample[0], 'output': sample[1]}])
88
  print(llmresult)
89
+ logprobs = llmresult.generations[0][0].generation_info['logprobs']
90
  print(logprobs)
91
+ tokens = logprobs['tokens']
92
+ token_probs = logprobs['token_logprobs']
93
  print(tokens)
94
+ print(token_probs)
95
+ score += sum(token_probs)
96
+ return score