()
| 462 | |
| 463 | # Create a streaming response |
| 464 | async def response_stream(): |
| 465 | try: |
| 466 | if request.provider == "ollama": |
| 467 | # Get the response and handle it properly using the previously created api_kwargs |
| 468 | response = await model.acall(api_kwargs=api_kwargs, model_type=ModelType.LLM) |
| 469 | # Handle streaming response from Ollama |
| 470 | async for chunk in response: |
| 471 | text = getattr(chunk, 'response', None) or getattr(chunk, 'text', None) or str(chunk) |
| 472 | if text and not text.startswith('model=') and not text.startswith('created_at='): |
| 473 | text = text.replace('<think>', '').replace('</think>', '') |
| 474 | yield text |
| 475 | elif request.provider == "openrouter": |
| 476 | try: |
| 477 | # Get the response and handle it properly using the previously created api_kwargs |
| 478 | logger.info("Making OpenRouter API call") |
| 479 | response = await model.acall(api_kwargs=api_kwargs, model_type=ModelType.LLM) |
| 480 | # Handle streaming response from OpenRouter |
| 481 | async for chunk in response: |
| 482 | yield chunk |
| 483 | except Exception as e_openrouter: |
| 484 | logger.error(f"Error with OpenRouter API: {str(e_openrouter)}") |
| 485 | yield f"\nError with OpenRouter API: {str(e_openrouter)}\n\nPlease check that you have set the OPENROUTER_API_KEY environment variable with a valid API key." |
| 486 | elif request.provider == "openai": |
| 487 | try: |
| 488 | # Get the response and handle it properly using the previously created api_kwargs |
| 489 | logger.info("Making Openai API call") |
| 490 | response = await model.acall(api_kwargs=api_kwargs, model_type=ModelType.LLM) |
| 491 | # Handle streaming response from Openai |
| 492 | async for chunk in response: |
| 493 | choices = getattr(chunk, "choices", []) |
| 494 | if len(choices) > 0: |
| 495 | delta = getattr(choices[0], "delta", None) |
| 496 | if delta is not None: |
| 497 | text = getattr(delta, "content", None) |
| 498 | if text is not None: |
| 499 | yield text |
| 500 | except Exception as e_openai: |
| 501 | logger.error(f"Error with Openai API: {str(e_openai)}") |
| 502 | yield f"\nError with Openai API: {str(e_openai)}\n\nPlease check that you have set the OPENAI_API_KEY environment variable with a valid API key." |
| 503 | elif request.provider == "bedrock": |
| 504 | try: |
| 505 | # Get the response and handle it properly using the previously created api_kwargs |
| 506 | logger.info("Making AWS Bedrock API call") |
| 507 | response = await model.acall(api_kwargs=api_kwargs, model_type=ModelType.LLM) |
| 508 | # Handle response from Bedrock (not streaming yet) |
| 509 | if isinstance(response, str): |
| 510 | yield response |
| 511 | else: |
| 512 | # Try to extract text from the response |
| 513 | yield str(response) |
| 514 | except Exception as e_bedrock: |
| 515 | logger.error(f"Error with AWS Bedrock API: {str(e_bedrock)}") |
| 516 | yield f"\nError with AWS Bedrock API: {str(e_bedrock)}\n\nPlease check that you have set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables with valid credentials." |
| 517 | elif request.provider == "azure": |
| 518 | try: |
| 519 | # Get the response and handle it properly using the previously created api_kwargs |
| 520 | logger.info("Making Azure AI API call") |
| 521 | response = await model.acall(api_kwargs=api_kwargs, model_type=ModelType.LLM) |
no test coverage detected