Execute the pipeline n times and return n responses. Args: n (int): Number of times to run the pipeline approaches (list): List of approaches to execute operation (str): Operation type ('SINGLE', 'AND', or 'OR') system_prompt (str): System prompt ini
(n: int, approaches, operation: str, system_prompt: str, initial_query: str, client: Any, model: str,
request_config: dict = None, request_id: str = None)
| 502 | return list(responses), sum(tokens) |
| 503 | |
| 504 | def execute_n_times(n: int, approaches, operation: str, system_prompt: str, initial_query: str, client: Any, model: str, |
| 505 | request_config: dict = None, request_id: str = None) -> Tuple[Union[str, List[str]], int]: |
| 506 | """ |
| 507 | Execute the pipeline n times and return n responses. |
| 508 | |
| 509 | Args: |
| 510 | n (int): Number of times to run the pipeline |
| 511 | approaches (list): List of approaches to execute |
| 512 | operation (str): Operation type ('SINGLE', 'AND', or 'OR') |
| 513 | system_prompt (str): System prompt |
| 514 | initial_query (str): Initial query |
| 515 | client: OpenAI client instance |
| 516 | model (str): Model identifier |
| 517 | |
| 518 | Returns: |
| 519 | Tuple[Union[str, List[str]], int]: List of responses and total token count |
| 520 | """ |
| 521 | responses = [] |
| 522 | total_tokens = 0 |
| 523 | |
| 524 | for _ in range(n): |
| 525 | if operation == 'SINGLE': |
| 526 | response, tokens = execute_single_approach(approaches[0], system_prompt, initial_query, client, model, request_config, request_id) |
| 527 | elif operation == 'AND': |
| 528 | response, tokens = execute_combined_approaches(approaches, system_prompt, initial_query, client, model, request_config) |
| 529 | elif operation == 'OR': |
| 530 | loop = asyncio.new_event_loop() |
| 531 | asyncio.set_event_loop(loop) |
| 532 | response, tokens = loop.run_until_complete(execute_parallel_approaches(approaches, system_prompt, initial_query, client, model, request_config)) |
| 533 | loop.close() |
| 534 | else: |
| 535 | raise ValueError(f"Unknown operation: {operation}") |
| 536 | |
| 537 | # If response is already a list (from OR operation), extend responses |
| 538 | # Otherwise append the single response |
| 539 | if isinstance(response, list): |
| 540 | responses.extend(response) |
| 541 | else: |
| 542 | responses.append(response) |
| 543 | total_tokens += tokens |
| 544 | |
| 545 | # If n=1 and we got a single response, return it as is |
| 546 | # Otherwise return the list of responses |
| 547 | if n == 1 and len(responses) == 1: |
| 548 | return responses[0], total_tokens |
| 549 | return responses, total_tokens |
| 550 | |
| 551 | def generate_streaming_response(final_response, model): |
| 552 | # Generate a unique response ID |
no test coverage detected