assistant_test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import openai
  2. from app.models.assistant import Assistant
  3. from app.providers.database import session
  4. # 测试创建动作
  5. def test_create_assistant_with_extra_body():
  6. client = openai.OpenAI(base_url="http://localhost:8086/api/v1", api_key="xxx")
  7. assistant = client.beta.assistants.create(
  8. name="Assistant Demo",
  9. instructions="你是一个有用的助手",
  10. extra_body={
  11. "extra_body": {
  12. "model_params": {
  13. "frequency_penalty": 0,
  14. "logit_bias": None,
  15. "max_tokens": 1024,
  16. "presence_penalty": 0.6,
  17. "temperature": 1,
  18. "presence_penalty": 0,
  19. "top_p": 1,
  20. }
  21. }
  22. },
  23. # https://platform.openai.com/docs/api-reference/chat/create 具体参数看这里
  24. model="gpt-3.5-turbo-1106",
  25. )
  26. query = session.query(Assistant).filter(Assistant.id == assistant.id)
  27. assistant = query.one()
  28. assert assistant.name == "Assistant Demo"
  29. assert assistant.instructions == "你是一个有用的助手"
  30. assert assistant.model == "gpt-3.5-turbo-1106"
  31. assert assistant.extra_body == {
  32. "model_params": {
  33. "frequency_penalty": 0,
  34. "logit_bias": None,
  35. "max_tokens": 1024,
  36. "presence_penalty": 0.6,
  37. "temperature": 1,
  38. "presence_penalty": 0,
  39. "top_p": 1,
  40. }
  41. }
  42. session.close()
  43. # test create assistants with metadata
  44. def test_create_assistant_with_metadata():
  45. client = openai.OpenAI(base_url="http://localhost:8086/api/v1", api_key="xxx")
  46. assistant = client.beta.assistants.create(
  47. name="Assistant Demo",
  48. instructions="你是一个有用的助手",
  49. metadata={"memory": {"type": "window", "window_size": 2, "max_token_size": 5}},
  50. model="gpt-3.5-turbo-1106",
  51. )
  52. query = session.query(Assistant).filter(Assistant.id == assistant.id)
  53. assistant = query.one()
  54. assert assistant.name == "Assistant Demo"
  55. assert assistant.instructions == "你是一个有用的助手"
  56. assert assistant.model == "gpt-3.5-turbo-1106"
  57. assert assistant.metadata_ == {"memory": {"type": "window", "window_size": 2, "max_token_size": 5}}
  58. session.close()
  59. def test_create_assistant_with_temperature_and_top_p():
  60. client = openai.OpenAI(base_url="http://localhost:8086/api/v1", api_key="xxx")
  61. assistant = client.beta.assistants.create(
  62. name="Assistant Demo",
  63. instructions="你是一个有用的助手",
  64. temperature=1,
  65. top_p=1,
  66. # https://platform.openai.com/docs/api-reference/chat/create 具体参数看这里
  67. model="gpt-3.5-turbo-1106",
  68. )
  69. query = session.query(Assistant).filter(Assistant.id == assistant.id)
  70. assistant = query.one()
  71. assert assistant.name == "Assistant Demo"
  72. assert assistant.instructions == "你是一个有用的助手"
  73. assert assistant.model == "gpt-3.5-turbo-1106"
  74. assert assistant.temperature == 1
  75. assert assistant.top_p == 1
  76. session.close()
  77. def test_update_assistant_with_temperature_and_top_p():
  78. client = openai.OpenAI(base_url="http://localhost:8086/api/v1", api_key="xxx")
  79. assistant = client.beta.assistants.create(
  80. name="Assistant Demo",
  81. instructions="你是一个有用的助手",
  82. temperature=1,
  83. top_p=1,
  84. # https://platform.openai.com/docs/api-reference/chat/create 具体参数看这里
  85. model="gpt-3.5-turbo-1106",
  86. )
  87. assistant = client.beta.assistants.update(assistant.id, temperature=2, top_p=0.9)
  88. query = session.query(Assistant).filter(Assistant.id == assistant.id)
  89. assistant = query.one()
  90. assert assistant.name == "Assistant Demo"
  91. assert assistant.instructions == "你是一个有用的助手"
  92. assert assistant.model == "gpt-3.5-turbo-1106"
  93. assert assistant.temperature == 2
  94. assert assistant.top_p == 0.9
  95. session.close()