infiniflow / ragflow

RAGFlow is an open-source RAG (Retrieval-Augmented Generation) engine based on deep document understanding.
https://ragflow.io
Apache License 2.0
19.97k stars 1.99k forks source link

[Question]: chat API Keeps Returning Error Message #1964

Open mighty-laolin opened 2 months ago

mighty-laolin commented 2 months ago

Describe your problem

We run RAGFlow v0.9.0 with docker on Linux. Everything runs OK with the WebUI, but when we switch to the APIs we keep getting the following response:

{'data': None, 'retcode': 101, 'retmsg': 'required argument are missing: conversation_id, messages; '}

Here's how we set up the conversation:

def start_conversation(user_id='znj'):
    url = base_url + 'api/new_conversation'
    param = {
        'user_id': user_id
    }
    response = requests.get(url, params=param, headers=headers)
    conversation_id = None
    msg = None
    if response.status_code == 200:
        content = response.json()
        conversation_id = content['data']['id']
        msg = content['data']['message'] # content role
    else:
        print(f"Request failed with status code: {response.status_code}")
    return response.status_code, conversation_id, msg

def get_answer(conversation_id, msg, doc_ids, quote=False, stream=True):
    url = base_url + 'api/completion'
    params = {
    "conversation_id": conversation_id,  # 替代为你自己的对话ID
    "messages": [{"role": "user", "content": msg}],  # 替代为你的消息内容
    "quote": False,
    "stream": True,
    "doc_ids": doc_ids # 如果有文档ID则替代为相应的值,否则删除或留空
    }
    headers_json = {"Content-Type": "application/json",
                    'Authorization': 'Bearer ' + api_key}

    try:
        response = requests.post(url=url, headers=headers_json, data=params)
        print(response.json())
        response.raise_for_status()  # Raises an HTTPError for bad responses (4xx and 5xx)
    except requests.exceptions.HTTPError as http_err:
        print(f"HTTP error occurred: {http_err}")
        return response.status_code, None, None
    except Exception as err:
        print(f"An error occurred: {err}")
        return None, None, None

    try:
        content = response.json()
        data = content.get('data', None)
        retcode = content.get('retcode', None)
        retmsg = content.get('retmsg', None)
        if data:
            answer = data.get('answer', None)
        else:
            answer = None
    except ValueError:
        print("Failed to parse JSON response")
        return response.status_code, None, None

    return response.status_code, answer, retmsg

def chat(document_id):
    status_code, conversation_id, msg = start_conversation()
    print(conversation_id)
    # conversation_id = input("Enter conversation ID: ")
    doc_ids = document_id
    print("Chatbot initialized. Type 'exit' to end the conversation.")

    while True:
        user_message = input("You: ")
        if user_message.lower() == 'exit':
            print("Ending the conversation.")
            break
        status_code, answer, retmsg = get_answer(conversation_id, user_message, doc_ids)
        if status_code is not None and status_code == 200 and answer:
            print(f"Chatbot: {answer}")
        elif retmsg:
            print(f"Failed to get a response from the chatbot. Error message: {retmsg}")
        else:
            print("Failed to get a response from the chatbot.")

We checked the conversation_id returned from start_conversation() and it was not empty. We are really confused why the server keeps returning the error messages, as everything seems to be in place.

KevinHuSh commented 2 months ago

I suggest to use dev version of docker image and debug it with postman at first place.

FinTechCao commented 2 months ago

I also encountered this same issue, and it’s present in the online demo version as well. And the docker image version is dev

guoyuhao2330 commented 2 months ago

Describe your problem

We run RAGFlow v0.9.0 with docker on Linux. Everything runs OK with the WebUI, but when we switch to the APIs we keep getting the following response:

{'data': None, 'retcode': 101, 'retmsg': 'required argument are missing: conversation_id, messages; '}

Here's how we set up the conversation:

def start_conversation(user_id='znj'):
    url = base_url + 'api/new_conversation'
    param = {
        'user_id': user_id
    }
    response = requests.get(url, params=param, headers=headers)
    conversation_id = None
    msg = None
    if response.status_code == 200:
        content = response.json()
        conversation_id = content['data']['id']
        msg = content['data']['message'] # content role
    else:
        print(f"Request failed with status code: {response.status_code}")
    return response.status_code, conversation_id, msg

def get_answer(conversation_id, msg, doc_ids, quote=False, stream=True):
    url = base_url + 'api/completion'
    params = {
    "conversation_id": conversation_id,  # 替代为你自己的对话ID
    "messages": [{"role": "user", "content": msg}],  # 替代为你的消息内容
    "quote": False,
    "stream": True,
    "doc_ids": doc_ids # 如果有文档ID则替代为相应的值,否则删除或留空
    }
    headers_json = {"Content-Type": "application/json",
                    'Authorization': 'Bearer ' + api_key}

    try:
        response = requests.post(url=url, headers=headers_json, data=params)
        print(response.json())
        response.raise_for_status()  # Raises an HTTPError for bad responses (4xx and 5xx)
    except requests.exceptions.HTTPError as http_err:
        print(f"HTTP error occurred: {http_err}")
        return response.status_code, None, None
    except Exception as err:
        print(f"An error occurred: {err}")
        return None, None, None

    try:
        content = response.json()
        data = content.get('data', None)
        retcode = content.get('retcode', None)
        retmsg = content.get('retmsg', None)
        if data:
            answer = data.get('answer', None)
        else:
            answer = None
    except ValueError:
        print("Failed to parse JSON response")
        return response.status_code, None, None

    return response.status_code, answer, retmsg

def chat(document_id):
    status_code, conversation_id, msg = start_conversation()
    print(conversation_id)
    # conversation_id = input("Enter conversation ID: ")
    doc_ids = document_id
    print("Chatbot initialized. Type 'exit' to end the conversation.")

    while True:
        user_message = input("You: ")
        if user_message.lower() == 'exit':
            print("Ending the conversation.")
            break
        status_code, answer, retmsg = get_answer(conversation_id, user_message, doc_ids)
        if status_code is not None and status_code == 200 and answer:
            print(f"Chatbot: {answer}")
        elif retmsg:
            print(f"Failed to get a response from the chatbot. Error message: {retmsg}")
        else:
            print("Failed to get a response from the chatbot.")

We checked the conversation_id returned from start_conversation() and it was not empty. We are really confused why the server keeps returning the error messages, as everything seems to be in place.

you can try response = requests.post(url=url, headers=headers_json, data=json.dumps(params))