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
| import json.decoder
import qianfan import os from utils.enums import LLM import time
def init_qianfan(QIANFAN_ACCESS_KEY, QIANFAN_SECRET_KEY,model): os.environ["QIANFAN_ACCESS_KEY"] = QIANFAN_ACCESS_KEY os.environ["QIANFAN_SECRET_KEY"] = QIANFAN_SECRET_KEY
def ask_completion(model, batch, temperature): completion = qianfan.Completion() response = completion.do( model=model, prompt=batch, temperature=temperature, max_output_tokens=200, top_p=1, frequency_penalty=0, presence_penalty=0, stop=[";"] ) response_clean = [response["result"]] return dict( response=response_clean, prompt_tokens=response["usage"]["prompt_tokens"], completion_tokens=response["usage"]["completion_tokens"], total_tokens=response["usage"]["total_tokens"] )
def ask_chat(model, messages: list, temperature, n): chat_completion = qianfan.ChatCompletion() response = chat_completion.do( model=model, messages=messages, temperature=temperature, max_output_tokens=200 ) response_clean = [response["result"]] if n == 1: response_clean = response_clean[0] return dict( response=response_clean, prompt_tokens=response["usage"]["prompt_tokens"], completion_tokens=response["usage"]["completion_tokens"], total_tokens=response["usage"]["total_tokens"] )
def ask_llm(model: str, batch: list, temperature: float, n: int): n_repeat = 0 while True: try: if model in LLM.TASK_COMPLETIONS: assert n == 1 response = ask_completion(model, batch, temperature) elif model in LLM.TASK_CHAT: assert len(batch) == 1, "batch must be 1 in this mode" messages = [{"role": "user", "content": batch[0]}] response = ask_chat(model, messages, temperature, n) response['response'] = [response['response']] break except json.decoder.JSONDecodeError: n_repeat += 1 print(f"Repeat for the {n_repeat} times for JSONDecodeError", end="\n") time.sleep(1) continue except Exception as e: n_repeat += 1 print(f"Repeat for the {n_repeat} times for exception: {e}", end="\n") time.sleep(1) continue
return response
|