| 8 | from copy import deepcopy |
| 9 | |
| 10 | def process_response(output, history): |
| 11 | content = "" |
| 12 | history = deepcopy(history) |
| 13 | for response in output.split("<|assistant|>"): |
| 14 | metadata, content = response.split("\n", maxsplit=1) |
| 15 | if not metadata.strip(): |
| 16 | content = content.strip() |
| 17 | history.append({"role": "assistant", "metadata": metadata, "content": content}) |
| 18 | content = content.replace("[[训练时间]]", "2023年") |
| 19 | else: |
| 20 | history.append({"role": "assistant", "metadata": metadata, "content": content}) |
| 21 | if history[0]["role"] == "system" and "tools" in history[0]: |
| 22 | content = "\n".join(content.split("\n")[1:-1]) |
| 23 | def tool_call(**kwargs): |
| 24 | return kwargs |
| 25 | parameters = eval(content) |
| 26 | content = {"name": metadata.strip(), "parameters": parameters} |
| 27 | else: |
| 28 | content = {"name": metadata.strip(), "content": content} |
| 29 | return content, history |
| 30 | |
| 31 | def chat(model, tokenizer, |
| 32 | max_length: int = 256, num_beams=1, top_p=0.7, top_k=0, temperature=0.95): |