[Python] creating POST in python, getting error " Exception: not enough values to unpack...

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    Given this code

    def run_chat_api(chat, chat_history):
    url = f"{BASE_URL}/wca/core/chat/text/generation"
    chat_history_str = json.dumps(chat_history)
    #chat_history = get_chat_history("chat_history.json")


    print("printing chat history")
    print(chat_history)

    print("printing URL")
    print(url)

    print("printing headers")
    print(headers)



    payload = {
    "message_payload": {
    "messages": [{"content": chat, "role": "USER"}],
    }
    }

    files = []

    files.append (payload)

    #encoded_content = base64.b64encode(chat_history_str.read()).decode('utf-8')
    files.append(('files', ("chat_history.json", chat_history_str, 'text/plain')))


    for element in files:
    print(element, end=" ")


    try:
    response = requests.post(
    url=url,
    headers=headers,
    files=files,
    timeout=180
    )

    if not response.ok:
    handle_error(response=response.content, url=url, request_id={REQUEST_ID})
    return response.json()
    except Exception as e:
    print("Exception: ")
    return print(e)


    Here is the printout:

    printing chat history
    [{'chat': 'write java to print "Hello WCA"', 'response': None}]
    printing URL
    https://api.dataplatform.cloud.ibm.com/v2/wca/core/chat/text/generation
    printing headers
    {'Content-Type': 'content-type: multipart/form-data', 'Authorization': 'Bearer 123456', 'Request-Id': 'request id: 6lubq6c3-6uee-90o1-oad730hnnvit'}
    {'message_payload': {'messages': [{'content': 'write java to print "Hello WCA"', 'role': 'USER'}]}} ('files', ('chat_history.json', '[{"chat": "write java to print \\"Hello WCA\\"", "response": null}]', 'text/plain')) **Exception:
    not enough values to unpack (expected 2, got 1)**


    What does this exception refer to?

    Rest of calling code:

    def main():
    st.title("WCA@IBM API Interactive Chat")
    # Load chat history
    chat_history = get_chat_history("/Users/kameronacole/PycharmProjects/WCAUseCase1/chat_history.json")
    #chat_history = get_chat_history("chat_history.json")
    # Create a text input for user chats
    user_chat = st.text_area("Enter your chat:")
    # Submit button
    if st.button("Submit"):
    if user_chat:
    # Append the new chat to the history
    chat_history.append({"chat": user_chat, "response": None})
    save_chat_history("chat_history.json", chat_history)
    # Run the chat API
    response = run_chat_api(user_chat, chat_history)
    # Display the response
    if 'response' in response:
    st.write(f"Response: {response['response']}")
    else:
    st.write("No response received from the API.")
    # Update the response for the latest chat entry
    if chat_history and 'response' in response:
    chat_history[-1]['response'] = response['response']
    # Add the chat and response to the history
    for entry in chat_history:
    st.write(f"Chat: {entry['chat']}\nResponse: {entry['response']}\n\n")
    # Set the BEARER_TOKEN in the session state
    if "BEARER_TOKEN" not in st.session_state:
    st.session_state.BEARER_TOKEN = BEARER_TOKEN


    if name == "main": main()

    Continue reading...

Compartilhe esta Página