[Feature Request]: No token in chat session with latest version on master branch?
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
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
It's here.
It's here.
But after change the new token, above demo code still not work. Could you please show me a demo how to use chat api?
Check this out.
It's here.
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! \ ""
Have the same problem! Do you know how to solve this?
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()
