mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
### What problem does this PR solve? Added OpenAI-like completion api, related to #4672, #4705 This function allows users to interact with a model to get responses based on a series of messages. If `stream` is set to True, the response will be streamed in chunks, mimicking the OpenAI-style API. #### Example usage: ```bash curl -X POST https://ragflow_address.com/api/v1/chats_openai/<chat_id>/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $RAGFLOW_API_KEY" \ -d '{ "model": "model", "messages": [{"role": "user", "content": "Say this is a test!"}], "stream": true }' ``` Alternatively, you can use Python's `OpenAI` client: ```python from openai import OpenAI model = "model" client = OpenAI(api_key="ragflow-api-key", base_url=f"http://ragflow_address/api/v1/chats_openai/<chat_id>") completion = client.chat.completions.create( model=model, messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Who you are?"}, {"role": "assistant", "content": "I am an AI assistant named..."}, {"role": "user", "content": "Can you tell me how to install neovim"}, ], stream=True ) stream = True if stream: for chunk in completion: print(chunk) else: print(completion.choices[0].message.content) ``` ### Type of change - [x] New Feature (non-breaking change which adds functionality) ### Related Issues Related to #4672, #4705