Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

set_chat_variables.md 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ---
  2. sidebar_position: 4
  3. slug: /set_chat_variables
  4. ---
  5. # Set variables
  6. Set variables to be used together with the system prompt for your LLM.
  7. ---
  8. When configuring the system prompt for a chat model, variables play an important role in enhancing flexibility and reusability. With variables, you can dynamically adjust the system prompt to be sent to your model. In the context of RAGFlow, if you have defined variables in the **Chat Configuration** dialogue, except for the system's reserved variable `{knowledge}`, you are required to pass in values for them from RAGFlow's [HTTP API](../../references/http_api_reference.md#converse-with-chat-assistant) or through its [Python SDK](../../references/python_api_reference.md#converse-with-chat-assistant).
  9. :::danger IMPORTANT
  10. In RAGFlow, variables are closely linked with the system prompt. When you add a variable in the **Variable** section, include it in the system prompt. Conversely, when deleting a variable, ensure it is removed from the system prompt; otherwise, an error would occur.
  11. :::
  12. ## Where to set variables
  13. Hover your mouse over your chat assistant, click **Edit** to open its **Chat Configuration** dialogue, then click the **Prompt engine** tab. Here, you can work on your variables in the **System prompt** field and the **Variable** section:
  14. ![set_variables](https://raw.githubusercontent.com/infiniflow/ragflow-docs/main/images/prompt_engine.jpg)
  15. ## 1. Manage variables
  16. In the **Variable** section, you add, remove, or update variables.
  17. ### `{knowledge}` - a reserved variable
  18. `{knowledge}` is the system's reserved variable, representing the chunks retrieved from the knowledge base(s) specified by **Knowledge bases** under the **Assistant settings** tab. If your chat assistant is associated with certain knowledge bases, you can keep it as is.
  19. :::info NOTE
  20. It currently makes no difference whether `{knowledge}` is set as optional or mandatory, but please note this design will be updated in due course.
  21. :::
  22. From v0.17.0 onward, you can start an AI chat without specifying knowledge bases. In this case, we recommend removing the `{knowledge}` variable to prevent unnecessary reference and keeping the **Empty response** field empty to avoid errors.
  23. ### Custom variables
  24. Besides `{knowledge}`, you can also define your own variables to pair with the system prompt. To use these custom variables, you must pass in their values through RAGFlow's official APIs. The **Optional** toggle determines whether these variables are required in the corresponding APIs:
  25. - **Disabled** (Default): The variable is mandatory and must be provided.
  26. - **Enabled**: The variable is optional and can be omitted if not needed.
  27. ## 2. Update system prompt
  28. After you add or remove variables in the **Variable** section, ensure your changes are reflected in the system prompt to avoid inconsistencies or errors. Here's an example:
  29. ```
  30. You are an intelligent assistant. Please answer the question by summarizing chunks from the specified knowledge base(s)...
  31. Your answers should follow a professional and {style} style.
  32. ...
  33. Here is the knowledge base:
  34. {knowledge}
  35. The above is the knowledge base.
  36. ```
  37. :::tip NOTE
  38. If you have removed `{knowledge}`, ensure that you thoroughly review and update the entire system prompt to achieve optimal results.
  39. :::
  40. ## APIs
  41. The *only* way to pass in values for the custom variables defined in the **Chat Configuration** dialogue is to call RAGFlow's [HTTP API](../../references/http_api_reference.md#converse-with-chat-assistant) or through its [Python SDK](../../references/python_api_reference.md#converse-with-chat-assistant).
  42. ### HTTP API
  43. See [Converse with chat assistant](../../references/http_api_reference.md#converse-with-chat-assistant). Here's an example:
  44. ```json {9}
  45. curl --request POST \
  46. --url http://{address}/api/v1/chats/{chat_id}/completions \
  47. --header 'Content-Type: application/json' \
  48. --header 'Authorization: Bearer <YOUR_API_KEY>' \
  49. --data-binary '
  50. {
  51. "question": "xxxxxxxxx",
  52. "stream": true,
  53. "style":"hilarious"
  54. }'
  55. ```
  56. ### Python API
  57. See [Converse with chat assistant](../../references/python_api_reference.md#converse-with-chat-assistant). Here's an example:
  58. ```python {18}
  59. from ragflow_sdk import RAGFlow
  60. rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
  61. assistant = rag_object.list_chats(name="Miss R")
  62. assistant = assistant[0]
  63. session = assistant.create_session()
  64. print("\n==================== Miss R =====================\n")
  65. print("Hello. What can I do for you?")
  66. while True:
  67. question = input("\n==================== User =====================\n> ")
  68. style = input("Please enter your preferred style (e.g., formal, informal, hilarious): ")
  69. print("\n==================== Miss R =====================\n")
  70. cont = ""
  71. for ans in session.ask(question, stream=True, style=style):
  72. print(ans.content[len(cont):], end='', flush=True)
  73. cont = ans.content
  74. ```