I’m trying to use the Dify API in my Python code to retrieve responses. However, I’m encountering an issue with the authorization. Here’s the error message I’m getting:
2023-12-29 19:34:58.421 | DEBUG | mywebgpt:make_completion:36 - Chat/Completions Nb Retries : 0
2023-12-29 19:35:01.169 | DEBUG | mywebgpt:make_completion:48 - Status Code : 200
2023-12-29 19:35:01.169 | ERROR | mywebgpt:make_completion:55 - Expecting value: line 1 column 1 (char 0)
2023-12-29 19:35:01.169 | DEBUG | mywebgpt:make_completion:36 - Chat/Completions Nb Retries : 1
2023-12-29 19:35:03.485 | DEBUG | mywebgpt:make_completion:48 - Status Code : 200
2023-12-29 19:35:03.485 | ERROR | mywebgpt:make_completion:55 - Expecting value: line 1 column 1 (char 0)
2023-12-29 19:35:03.485 | DEBUG | mywebgpt:make_completion:36 - Chat/Completions Nb Retries : 2
2023-12-29 19:35:05.569 | DEBUG | mywebgpt:make_completion:48 - Status Code : 200
2023-12-29 19:35:05.569 | ERROR | mywebgpt:make_completion:55 - Expecting value: line 1 column 1 (char 0)
Here’s the relevant part of my code:
import gradio as gr
import asyncio, httpx
import async_timeout
from loguru import logger
from typing import Optional, List
from pydantic import BaseModel
import os
from dotenv import load_dotenv
load_dotenv()
class Message(BaseModel):
role: str
content: str
async def make_completion(messages:List[Message], nb_retries:int=3, delay:int=30) -> Optional[str]:
header = {
"Content-Type": "application/json",
"Authorization": "Bearer app-xxxxxxxxxxxxxxxxxxx"
}
try:
async with async_timeout.timeout(delay=delay):
async with httpx.AsyncClient(headers=header) as aio_client:
counter = 0
keep_loop = True
while keep_loop:
logger.debug(f"Chat/Completions Nb Retries : {counter}")
try:
resp = await aio_client.post(
url = "https://api.dify.ai/v1/chat-messages", # Update the API endpoint
json = {
"inputs": {},
"query": messages[-1]["content"], # Use the last message as the query
"response_mode": "streaming",
"conversation_id": "",
"user": "abc-123"
}
)
logger.debug(f"Status Code : {resp.status_code}")
if resp.status_code == 200:
return resp.json()["choices"][0]["message"]["content"]
else:
logger.warning(resp.content)
keep_loop = False
except Exception as e:
logger.error(e)
counter = counter + 1
keep_loop = counter < nb_retries
except asyncio.TimeoutError as e:
logger.error(f"Timeout {delay} seconds !")
return None
async def predict(input, history):
history.append({"role": "user", "content": input})
response = await make_completion(history)
history.append({"role": "assistant", "content": response})
messages = [(history[i]["content"], history[i+1]["content"]) for i in range(0, len(history)-1, 2)]
return messages, history
with gr.Blocks() as demo:
logger.info("Starting Demo...")
chatbot = gr.Chatbot(label="MyBrain")
state = gr.State([])
with gr.Row():
txt = gr.Textbox(show_label=False, placeholder="Type your question and press enter").style(container=False)
txt.submit(predict, [txt, state], [chatbot, state])
demo.launch(server_port=8080)
I’ve checked my API key and it seems to be correct. I’ve also tried regenerating the key but the issue persists. Any help would be greatly appreciated!