(ds, output_path)
| 190 | pbar.update(1) |
| 191 | |
| 192 | def kimi_api(ds, output_path): |
| 193 | if MOONSHOT_API_KEY is None: |
| 194 | print("Please set MOONSHOT_API_KEY environment variable.") |
| 195 | return |
| 196 | client = OpenAI(base_url="https://api.moonshot.cn/v1", api_key=MOONSHOT_API_KEY) |
| 197 | MAX_WORKERS = 4 |
| 198 | |
| 199 | def search_impl(arguments: Dict[str, Any]) -> Any: |
| 200 | return arguments |
| 201 | |
| 202 | def chat(messages) -> Choice: |
| 203 | try: |
| 204 | completion = client.chat.completions.create( |
| 205 | model="moonshot-v1-128k", |
| 206 | messages=messages, |
| 207 | temperature=0.3, |
| 208 | tools=[{ |
| 209 | "type": "builtin_function", |
| 210 | "function": {"name": "$web_search"} |
| 211 | }] |
| 212 | ) |
| 213 | return completion.choices[0] |
| 214 | except Exception as e: |
| 215 | print(f"Error: {e}") |
| 216 | return |
| 217 | |
| 218 | if not os.path.exists(output_path): |
| 219 | open(output_path, "w").close() |
| 220 | |
| 221 | visited = set() |
| 222 | with open(output_path, "r", encoding="utf-8") as f: |
| 223 | visited.update(json.loads(line)["question"] for line in f) |
| 224 | |
| 225 | data_list = [] |
| 226 | for item in ds["question"]: |
| 227 | if item not in visited: |
| 228 | temp = {"question": item} |
| 229 | data_list.append(temp) |
| 230 | def process_data(data): |
| 231 | messages = [{"role": "system", "content": "你是 Kimi。"}, {"role": "user", "content": data["question"]}] |
| 232 | finish_reason = None |
| 233 | while finish_reason is None or finish_reason == "tool_calls": |
| 234 | choice = chat(messages) |
| 235 | finish_reason = choice.finish_reason |
| 236 | if finish_reason == "tool_calls": |
| 237 | messages.append(choice.message) |
| 238 | for tool_call in choice.message.tool_calls: |
| 239 | tool_result = search_impl(json.loads(tool_call.function.arguments)) |
| 240 | messages.append({ |
| 241 | "role": "tool", |
| 242 | "tool_call_id": tool_call.id, |
| 243 | "name": tool_call.function.name, |
| 244 | "content": json.dumps(tool_result), |
| 245 | }) |
| 246 | data["pred"] = choice.message.content |
| 247 | return data |
| 248 | |
| 249 | with tqdm(total=len(data_list)) as pbar: |
nothing calls this directly
no outgoing calls
no test coverage detected