(
question: dict, model: str, num_choices: int, max_tokens: int, answer_file: str
)
| 25 | |
| 26 | |
| 27 | def get_answer( |
| 28 | question: dict, model: str, num_choices: int, max_tokens: int, answer_file: str |
| 29 | ): |
| 30 | assert ( |
| 31 | args.force_temperature is not None and "required_temperature" in question.keys() |
| 32 | ) == False |
| 33 | if args.force_temperature is not None: |
| 34 | temperature = args.force_temperature |
| 35 | elif "required_temperature" in question.keys(): |
| 36 | temperature = question["required_temperature"] |
| 37 | elif question["category"] in temperature_config: |
| 38 | temperature = temperature_config[question["category"]] |
| 39 | else: |
| 40 | temperature = 0.7 |
| 41 | |
| 42 | choices = [] |
| 43 | chat_state = None # for palm-2 model |
| 44 | for i in range(num_choices): |
| 45 | conv = get_conversation_template(model) |
| 46 | |
| 47 | turns = [] |
| 48 | for j in range(len(question["turns"])): |
| 49 | conv.append_message(conv.roles[0], question["turns"][j]) |
| 50 | conv.append_message(conv.roles[1], None) |
| 51 | |
| 52 | if model in ANTHROPIC_MODEL_LIST: |
| 53 | output = chat_completion_anthropic(model, conv, temperature, max_tokens) |
| 54 | elif model == "palm-2-chat-bison-001": |
| 55 | chat_state, output = chat_completion_palm( |
| 56 | chat_state, model, conv, temperature, max_tokens |
| 57 | ) |
| 58 | else: |
| 59 | output = chat_completion_openai(model, conv, temperature, max_tokens) |
| 60 | |
| 61 | conv.update_last_message(output) |
| 62 | turns.append(output) |
| 63 | |
| 64 | choices.append({"index": i, "turns": turns}) |
| 65 | |
| 66 | # Dump answers |
| 67 | ans = { |
| 68 | "question_id": question["question_id"], |
| 69 | "answer_id": shortuuid.uuid(), |
| 70 | "model_id": model, |
| 71 | "choices": choices, |
| 72 | "tstamp": time.time(), |
| 73 | } |
| 74 | |
| 75 | os.makedirs(os.path.dirname(answer_file), exist_ok=True) |
| 76 | with open(answer_file, "a") as fout: |
| 77 | fout.write(json.dumps(ans) + "\n") |
| 78 | |
| 79 | |
| 80 | if __name__ == "__main__": |
nothing calls this directly
no test coverage detected
searching dependent graphs…