ragflow icon indicating copy to clipboard operation
ragflow copied to clipboard

[Feature Request]: No token in chat session with latest version on master branch?

Open galen1980guo opened this issue 1 year ago • 6 comments

Is there an existing issue for the same feature request?

  • [X] I have checked the existing issues.

Is your feature request related to a problem?

I can not find the token api in chat session now with latest version on master branch.

Describe the feature you'd like

image

Describe implementation you've considered

No response

Documentation, adoption, use case

I can use the chat session tokens last week.

import requests
import streamlit as st
import json

base_url = "http://10.110.0.25/v1/api/conversation"  
api_key_model_1 = "ragflow-QxZDVjODllODllZTExZWZiNThmMDI0Mm"  

headers_model_1 = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key_model_1}"
}

def create_new_conversation(headers):
    url = f"{base_url}/new_conversation"
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        conversation_data = response.json()
        conversation_id = conversation_data.get("data", {}).get("id")
        if conversation_id:
            print(f"New conversation ID: {conversation_id}") 
            return conversation_id
        else:
            st.error("API response does not contain a valid conversation ID.")
            return None
    else:
        st.error(f"Request failed with status code: {response.status_code}")
        st.error(f"Response content: {response.text}")
        return None

def get_completion(conversation_id, message, context_messages, headers, model_name):
    url_completion = f"{base_url}/completion"
    messages = context_messages + [{"role": "user", "content": message}]
    
    data = {
        "conversation_id": conversation_id,
        "messages": messages,  
        "model": model_name  
    }

    response = requests.post(url_completion, json=data, headers=headers)

    print("Response status code:", response.status_code) 
    print("Response content:", response.text)  

    if response.status_code == 200:
        try:
            last_answer = ""
            for line in response.text.splitlines():
                if line.startswith("data:"):
                    line_content = line[len("data:"):]
                    try:
                        json_data = json.loads(line_content)
                        if "data" in json_data and isinstance(json_data["data"], dict):
                            last_answer = json_data["data"].get("answer", "")
                        elif json_data["data"] is True:
                            break
                    except json.JSONDecodeError as e:
                        st.error(f"Failed to parse JSON: {e}")
                        continue
            return last_answer if last_answer else "Model returned no valid answer."
        except ValueError as e:
            st.error(f"Failed to parse JSON: {e}")
            return None
    else:
        st.error(f"Request failed with status code: {response.status_code}")
        st.error(f"Response content: {response.text}")
        return None

def main():
    st.title("AI Fusa Agent")

    if 'conversation_id' not in st.session_state:
        st.session_state.conversation_id = None
        st.session_state.context_messages = []  

    user_question = st.text_input("Input your question:")

    if st.button("Submit"):
        if not st.session_state.conversation_id:
            st.write("Creating new conversation...")
            st.session_state.conversation_id = create_new_conversation(headers_model_1)

        if st.session_state.conversation_id:
            st.write("Fetching the answer from the model...")
            answer = get_completion(st.session_state.conversation_id, user_question, st.session_state.context_messages, headers_model_1, "your_model_name_1")

            st.session_state.context_messages.append({"role": "user", "content": user_question})
            st.session_state.context_messages.append({"role": "assistant", "content": answer})

            for msg in st.session_state.context_messages:
                if msg["role"] == "user":
                    st.write(f"**You**: {msg['content']}")
                else:
                    st.write(f"**AI**: {msg['content']}")

if __name__ == "__main__":
    main()

Additional information

No response

galen1980guo avatar Oct 22 '24 17:10 galen1980guo

It's here. image

KevinHuSh avatar Oct 23 '24 02:10 KevinHuSh

It's here. image

But after change the new token, above demo code still not work. Could you please show me a demo how to use chat api?

galen1980guo avatar Oct 23 '24 03:10 galen1980guo

Check this out.

KevinHuSh avatar Oct 24 '24 01:10 KevinHuSh

It's here. image

But after change the new token, above demo code still not work. Could you please show me a demo how to use chat api?

Have you solved it yet? I encountered the same error as you, the dev version of the api generation seems to be wrong, showing "Token is not valid! \ "" Uploading Snipaste_2024-10-28_09-48-35.png…

brilliant-plus avatar Oct 28 '24 01:10 brilliant-plus

Have the same problem! Do you know how to solve this?

SnailKiller avatar Nov 01 '24 06:11 SnailKiller

Hi @galen1980guo , I've changed your code, and it works well for me, it seems some urls even the base url are changed. ps: I am running the RAGFlow docker version. Code is here. Hope it works. @SnailKiller


import requests
import streamlit as st
import json
chat_id = "9eb5dc359c3311ef91720242ac120006"
base_url = f"http://localhost:81/api/v1/chats/{chat_id}"
api_key_model_1 = "ragflow-I3HSM0NzI3OTgxODExZWY4MmI4MDI0Mm"

headers_model_1 = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key_model_1}",
}


def create_new_conversation(headers):
    url = f"{base_url}/sessions"
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        conversation_data = response.json()
        print(conversation_data)
        data =conversation_data.get("data", {})
        if type(data) == list:
            conversation_id = conversation_data.get("data", {})[0].get("id")
        else:
            conversation_id = conversation_data.get("data", {}).get("id")
        if conversation_id:
            print(f"New conversation ID: {conversation_id}")
            return conversation_id
        else:
            st.error("API response does not contain a valid conversation ID.")
            return None
    else:
        st.error(f"Request failed with status code: {response.status_code}")
        st.error(f"Response content: {response.text}")
        return None


def get_completion(conversation_id, message, context_messages, headers, model_name):
    url_completion = f"{base_url}/completions"
    # messages = context_messages + [{"role": "user", "content": message}]

    data = {
        "session_id": conversation_id,
        "question": message,
        "stream": False
    }
    print(f"Request data:{data}" )

    response = requests.post(url_completion, json=data, headers=headers, timeout=100000)
    response_data = json.loads(response.content.decode('utf-8'))
    print(f"Response status code:{response.status_code}" )
    print(f"Response content:{response_data}" )

    if response.status_code == 200:
        try:
            last_answer = response_data.get("data",{}).get("answer",None)
            return last_answer if last_answer else "Model returned no valid answer."
        except ValueError as e:
            st.error(f"Failed to parse JSON: {e}")
            return None
    else:
        st.error(f"Request failed with status code: {response.status_code}")
        st.error(f"Response content: {response.text}")
        return None


def main():
    st.title("AI Fusa Agent")

    if "conversation_id" not in st.session_state:
        st.session_state.conversation_id = None
        st.session_state.context_messages = []

    user_question = st.text_input("Input your question:")

    if st.button("Submit"):
        if not st.session_state.conversation_id:
            st.write("Creating new conversation...")
            st.session_state.conversation_id = create_new_conversation(headers_model_1)

        if st.session_state.conversation_id:
            st.write("Fetching the answer from the model...")
            answer = get_completion(
                st.session_state.conversation_id,
                user_question,
                st.session_state.context_messages,
                headers_model_1,
                "your_model_name_1",
            )

            st.session_state.context_messages.append(
                {"role": "user", "content": user_question}
            )
            st.session_state.context_messages.append(
                {"role": "assistant", "content": answer}
            )

            for msg in st.session_state.context_messages:
                if msg["role"] == "user":
                    st.write(f"**You**: {msg['content']}")
                else:
                    st.write(f"**AI**: {msg['content']}")


if __name__ == "__main__":
    main()

KingWin avatar Nov 05 '24 03:11 KingWin