public.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import warnings
  2. import requests
  3. from openhands.security.options import SecurityAnalyzers
  4. with warnings.catch_warnings():
  5. warnings.simplefilter('ignore')
  6. import litellm
  7. from fastapi import (
  8. APIRouter,
  9. )
  10. from openhands.controller.agent import Agent
  11. from openhands.core.config import LLMConfig
  12. from openhands.core.logger import openhands_logger as logger
  13. from openhands.llm import bedrock
  14. from openhands.server.shared import config
  15. app = APIRouter(prefix='/api/options')
  16. @app.get('/models')
  17. async def get_litellm_models() -> list[str]:
  18. """
  19. Get all models supported by LiteLLM.
  20. This function combines models from litellm and Bedrock, removing any
  21. error-prone Bedrock models.
  22. To get the models:
  23. ```sh
  24. curl http://localhost:3000/api/litellm-models
  25. ```
  26. Returns:
  27. list: A sorted list of unique model names.
  28. """
  29. litellm_model_list = litellm.model_list + list(litellm.model_cost.keys())
  30. litellm_model_list_without_bedrock = bedrock.remove_error_modelId(
  31. litellm_model_list
  32. )
  33. # TODO: for bedrock, this is using the default config
  34. llm_config: LLMConfig = config.get_llm_config()
  35. bedrock_model_list = []
  36. if (
  37. llm_config.aws_region_name
  38. and llm_config.aws_access_key_id
  39. and llm_config.aws_secret_access_key
  40. ):
  41. bedrock_model_list = bedrock.list_foundation_models(
  42. llm_config.aws_region_name,
  43. llm_config.aws_access_key_id,
  44. llm_config.aws_secret_access_key,
  45. )
  46. model_list = litellm_model_list_without_bedrock + bedrock_model_list
  47. for llm_config in config.llms.values():
  48. ollama_base_url = llm_config.ollama_base_url
  49. if llm_config.model.startswith('ollama'):
  50. if not ollama_base_url:
  51. ollama_base_url = llm_config.base_url
  52. if ollama_base_url:
  53. ollama_url = ollama_base_url.strip('/') + '/api/tags'
  54. try:
  55. ollama_models_list = requests.get(ollama_url, timeout=3).json()[
  56. 'models'
  57. ]
  58. for model in ollama_models_list:
  59. model_list.append('ollama/' + model['name'])
  60. break
  61. except requests.exceptions.RequestException as e:
  62. logger.error(f'Error getting OLLAMA models: {e}', exc_info=True)
  63. return list(sorted(set(model_list)))
  64. @app.get('/agents')
  65. async def get_agents():
  66. """Get all agents supported by LiteLLM.
  67. To get the agents:
  68. ```sh
  69. curl http://localhost:3000/api/agents
  70. ```
  71. Returns:
  72. list: A sorted list of agent names.
  73. """
  74. agents = sorted(Agent.list_agents())
  75. return agents
  76. @app.get('/security-analyzers')
  77. async def get_security_analyzers():
  78. """Get all supported security analyzers.
  79. To get the security analyzers:
  80. ```sh
  81. curl http://localhost:3000/api/security-analyzers
  82. ```
  83. Returns:
  84. list: A sorted list of security analyzer names.
  85. """
  86. return sorted(SecurityAnalyzers.keys())