fallbacks.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from litellm import Router
  2. import os
  3. from conf.config import G4F_API
  4. from litellm import embedding
  5. from litellm import completion
  6. g4f_api_base = G4F_API + 'v1'
  7. g4f_sv_v2_api_base = "http://sv-v2:1337/v1"
  8. # response = embedding(
  9. # model = "text-embedding-ada-002", # add `openai/` prefix to model so litellm knows to route to OpenAI
  10. # api_base="https://api.chatanywhere.com.cn" , # set API Base of your Custom OpenAI Endpoint
  11. # api_key= "sk-SG7fZHFePis7ymx3Em58eb0prCjgUE2t3mplbU0iq54IBo1k",
  12. # headers={"Authorization": "Bearer sk-SG7fZHFePis7ymx3Em58eb0prCjgUE2t3mplbU0iq54IBo1k"},
  13. # input=["good morning from litellm"]
  14. # )
  15. # print(f"response embedding {response}")
  16. # https://docs.litellm.ai/docs/routing#fallbacks
  17. model_list = [
  18. { # list of model deployments
  19. "model_name": "g4f/gpt-3.5-turbo", # openai model name
  20. "litellm_params": { # params for litellm completion/embedding call
  21. "model": "gpt-3.5-turbo",
  22. "api_key": "123",
  23. "api_base": g4f_api_base + 'bad'
  24. },
  25. },
  26. { # list of model deployments
  27. "model_name": "g4f-sv-v2/gpt-3.5-turbo", # openai model name
  28. "litellm_params": { # params for litellm completion/embedding call
  29. "model": "gpt-3.5-turbo",
  30. "api_key": "123",
  31. "api_base": g4f_sv_v2_api_base + 'bad'
  32. },
  33. },
  34. { # list of model deployments
  35. "model_name": "chatanywhere/gpt-3.5-turbo", # openai model name
  36. "litellm_params": { # params for litellm completion/embedding call
  37. "model": "gpt-3.5-turbo",
  38. "api_key": "sk-SG7fZHFePis7ymx3Em58eb0prCjgUE2t3mplbU0iq54IBo1k",
  39. "api_base": "https://api.chatanywhere.com.cn/v1" + 'bad',
  40. },
  41. },
  42. {
  43. "model_name": "qwen", # openai model name
  44. "litellm_params": { # params for litellm completion/embedding call
  45. "model": "gpt-3.5-turbo",
  46. "api_key": "123",
  47. "api_version": "",
  48. "api_base": "http://sv-v2:8002/v1",
  49. # 在 litellm.main compelte 中可以看到 litellm_params
  50. "extra_headers": {
  51. "Authorization": "Bearer OJVVpfICFrI*UaHrrXUaI5WM5mLOWAKQsEtTrD7Jugq8YUttvJSy3R0QqXzmvDrK0"
  52. }
  53. },
  54. },
  55. {
  56. "model_name": "kimi", # openai model name
  57. "litellm_params": { # params for litellm completion/embedding call
  58. "model": "gpt-3.5-turbo",
  59. "api_key": "123",
  60. "api_base": "http://sv-v2:8004/v1/",
  61. # 在 litellm.main compelte 中可以看到 litellm_params
  62. "extra_headers": {
  63. "Authorization": "Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyLWNlbnRlciIsImV4cCI6MTcyMDgxMzc3OSwiaWF0IjoxNzEzMDM3Nzc5LCJqdGkiOiJjb2RlM2twa3FxNHR0cmw3djRlMCIsInR5cCI6InJlZnJlc2giLCJzdWIiOiJjb2FxN3Jwa3FxNHR0cmkxdHRiZyIsInNwYWNlX2lkIjoiY29hcTdycGtxcTR0dHJpMXR0YjAiLCJhYnN0cmFjdF91c2VyX2lkIjoiY29hcTdycGtxcTR0dHJpMXR0YWcifQ.ZaCKPbKgbmwnkcSqn-w3s_fMlx0C956guZL9dO7bPMyU2GQ8be0DLoAG82a2IkMMU2XgKIRBwNOtkGX9fHMiTg"
  64. }
  65. },
  66. },
  67. {
  68. "model_name": "chatgml4", # openai model name
  69. "litellm_params": { # params for litellm completion/embedding call
  70. "model": "gpt-3.5-turbo",
  71. "api_key": "123",
  72. "api_base": "http://sv-v2:8003/v1/",
  73. # 在 litellm.main compelte 中可以看到 litellm_params
  74. "extra_headers": {
  75. "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTcxMDgyNTY0MCwianRpIjoiNDUzMzMyNGUtNzI1ZS00ZWM1LTgyMWYtMDM4ODgzMGVkMTAxIiwidHlwZSI6InJlZnJlc2giLCJzdWIiOiI5NjRjZjI4NGQzN2E0MWVlOWE3ZmMxNDMxYWU4YjBjZiIsIm5iZiI6MTcxMDgyNTY0MCwiZXhwIjoxNzI2Mzc3NjQwLCJ1aWQiOiI2NGQyYjAxMjU3MDlhODQ4YTMyMTE0ZGIiLCJ1cGxhdGZvcm0iOiIiLCJyb2xlcyI6WyJ1bmF1dGhlZF91c2VyIl19.zVf7GZpwICO0jhhR1dpqhLh6xoufmJmHGMmOVhL1fEA"
  76. }
  77. },
  78. },
  79. {
  80. "model_name": "yiyan", # openai model name
  81. "litellm_params": { # params for litellm completion/embedding call
  82. "model": "gpt-3.5-turbo",
  83. "provider":"YiYan4",
  84. "api_key": "123",
  85. "api_version": "",
  86. "api_base": "http://pc:8003/v1/",
  87. # 在 litellm.main compelte 中可以看到 litellm_params
  88. },
  89. },
  90. ]
  91. router = Router(model_list=model_list,
  92. fallbacks=[{"g4f/gpt-3.5-turbo": ["gpt-3.5-turbo"]},
  93. {"g4f-sv-v2/gpt-3.5-turbo": ["gpt-3.5-turbo"]},
  94. {"chatanywhere/gpt-3.5-turbo": ["gpt-3.5-turbo"]},
  95. {"qwen": ["gpt-3.5-turbo"]},
  96. {"kimi": ["gpt-3.5-turbo"]},
  97. {"chatgml4": ["gpt-3.5-turbo"]},
  98. ],
  99. context_window_fallbacks=[{"kimi": ["gpt-3.5-turbo"]}, {"chatgml4": ["gpt-3.5-turbo"]}],
  100. set_verbose=True)
  101. def main():
  102. user_message = "你是"
  103. messages = [{"content": user_message, "role": "user"}]
  104. # normal fallback call
  105. # response = router.completion(model="g4f/gpt-3.5-turbo", messages=messages,)
  106. response = completion(model_list=model_list, model="yiyan", messages=messages,)
  107. print(f"response: {response}")
  108. if __name__ == "__main__":
  109. main()