Response not rendering in the assistance block using Anthropic API

If you’re creating a debugging post, please include the following info:

  1. I am running this app Locally
import os
import streamlit as st
from anthropic import Anthropic

st.title("Odia Lingua")

# Ensure the API key is loaded from Streamlit secrets
client = Anthropic(api_key=st.secrets["ANTHROPIC_API_KEY"])

if "anthropic_model" not in st.session_state:
    st.session_state["anthropic_model"] = "claude-3-opus-20240229"

if "messages" not in st.session_state:
    st.session_state.messages = []

initial_prompt = """ଆପଣ ଏକ ଓଡ଼ିଆ ଭାଷା ବାର୍ତ୍ତାଳାପ AI ସହାୟକ। ଆପଣ କେବଳ ଓଡ଼ିଆ ଭାଷାରେ ଯୋଗାଯୋଗ କରିବେ ଏବଂ ଭାଷାର ବ୍ୟାକରଣ ନିୟମ ଏବଂ ସାଂସ୍କୃତିକ ଚଳଣୀଗୁଡ଼ିକୁ ଅନୁସରଣ କରିବେ। ଆପଣଙ୍କର ପ୍ରତିକ୍ରିୟାଗୁଡ଼ିକ ବିନମ୍ର, ସମ୍ମାନଜନକ ଏବଂ ଓଡ଼ିଆ ଭାଷାଭାଷୀ ପ୍ରେକ୍ଷାପଟକୁ ଅନୁକୂଳ ହେବା ଉଚିତ୍। ଆପଣଙ୍କର ଓଡ଼ିଶା ଓ ଓଡ଼ିଆ ଭାଷା ସମ୍ପର୍କରେ ଗଭୀର ବୁଝାମଣା ରହିଛି ଏବଂ ଆପଣ ବିଭିନ୍ନ ବିଷୟ ଉପରେ ଆଲୋଚନା କରିପାରିବେ। ବ୍ୟବହାରକାରୀଙ୍କ ଇନପୁଟ୍‌କୁ ପ୍ରତିକ୍ରିୟା କରିବା ସମୟରେ, ଆପଣ ପ୍ରସଙ୍ଗ ବଜାୟ ରଖିବା ପାଇଁ ବ୍ୟବହାରକାରୀଙ୍କ ବାର୍ତ୍ତାକୁ ଆପଣଙ୍କ ପ୍ରତିକ୍ରିୟାରେ ସାମିଲ କରିବେ ଏବଂ ପ୍ରାକୃତିକ ଶୁଣାଯାଉଥିବା ପ୍ରତିକ୍ରିୟା ପ୍ରଦାନ କରିବେ।"""

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

if prompt := st.chat_input("କେମିତି ଅଛନ୍ତି? ଆପଣଙ୍କ ପ୍ରଶ୍ନକୁ ଏଠାରେ ଲେଖନ୍ତୁ..."):
    if len(st.session_state.messages) > 0 and st.session_state.messages[-1]["role"] == "user":
        st.session_state.messages[-1]["content"] += "\n" + prompt
    else:
        st.session_state.messages.append({"role": "user", "content": prompt})

    with st.chat_message("user"):
        st.markdown(prompt)

    with st.chat_message("assistant"):
        # Ensure roles alternate correctly
        messages = [{"role": "user", "content": initial_prompt}] + st.session_state.messages
        if len(messages) > 1 and messages[-1]["role"] == "user" and messages[-2]["role"] == "user":
            messages.pop(-1)  # Remove the last "user" message
        # Send the corrected messages list to the API
        stream = client.messages.create(
            model=st.session_state["anthropic_model"],
            max_tokens=1024,
            system="",
            messages=messages,
            stream=True,
        )
        response = ""
        for event in stream:
            if event.type == "completion":
                response += event.data['text']
        st.markdown(response)
    st.session_state.messages.append({"role": "assistant", "content": response})

The API is working fine as i see in the dashboard of anthropic but the response is not rendering in the client side please Help

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.