Package: dify-client
dify-clientA Dify App Service-API Client, using for build a webapp by request Service-APIUsageFirst, installdify-clientpython sdk package:pip install dify-clientWrite your code with sdk:completion generate withblockingresponse_modefromdify_clientimportCompletionClientapi_key="your_api_key"# Initialize CompletionClientcompletion_client=CompletionClient(api_key)# Create Completion Message using CompletionClientcompletion_response=completion_client.create_completion_message(inputs={"query":"What's the weather like today?"},response_mode="blocking",user="user_id")completion_response.raise_for_status()result=completion_response.json()print(result.get('answer'))completion using vision model, like gpt-4-visionfromdify_clientimportCompletionClientapi_key="your_api_key"# Initialize CompletionClientcompletion_client=CompletionClient(api_key)files=[{"type":"image","transfer_method":"remote_url","url":"your_image_url"}]# files = [{#     "type": "image",#     "transfer_method": "local_file",#     "upload_file_id": "your_file_id"# }]# Create Completion Message using CompletionClientcompletion_response=completion_client.create_completion_message(inputs={"query":"Describe the picture."},response_mode="blocking",user="user_id",files=files)completion_response.raise_for_status()result=completion_response.json()print(result.get('answer'))chat generate withstreamingresponse_modeimportjsonfromdify_clientimportChatClientapi_key="your_api_key"# Initialize ChatClientchat_client=ChatClient(api_key)# Create Chat Message using ChatClientchat_response=chat_client.create_chat_message(inputs={},query="Hello",user="user_id",response_mode="streaming")chat_response.raise_for_status()forlineinchat_response.iter_lines(decode_unicode=True):line=line.split('data:',1)[-1]ifline.strip():line=json.loads(line.strip())print(line.get('answer'))chat using vision model, like gpt-4-visionfromdify_clientimportChatClientapi_key="your_api_key"# Initialize ChatClientchat_client=ChatClient(api_key)files=[{"type":"image","transfer_method":"remote_url","url":"your_image_url"}]# files = [{#     "type": "image",#     "transfer_method": "local_file",#     "upload_file_id": "your_file_id"# }]# Create Chat Message using ChatClientchat_response=chat_client.create_chat_message(inputs={},query="Describe the picture.",user="user_id",response_mode="blocking",files=files)chat_response.raise_for_status()result=chat_response.json()print(result.get("answer"))upload file when using vision modelfromdify_clientimportDifyClientapi_key="your_api_key"# Initialize Clientdify_client=DifyClient(api_key)file_path="your_image_file_path"file_name="panda.jpeg"mime_type="image/jpeg"withopen(file_path,"rb")asfile:files={"file":(file_name,file,mime_type)}response=dify_client.file_upload("user_id",files)result=response.json()print(f'upload_file_id:{result.get("id")}')Othersfromdify_clientimportChatClientapi_key="your_api_key"# Initialize Clientclient=ChatClient(api_key)# Get App parametersparameters=client.get_application_parameters(user="user_id")parameters.raise_for_status()print('[parameters]')print(parameters.json())# Get Conversation List (only for chat)conversations=client.get_conversations(user="user_id")conversations.raise_for_status()print('[conversations]')print(conversations.json())# Get Message List (only for chat)messages=client.get_conversation_messages(user="user_id",conversation_id="conversation_id")messages.raise_for_status()print('[messages]')print(messages.json())# Rename Conversation (only for chat)rename_conversation_response=client.rename_conversation(conversation_id="conversation_id",name="new_name",user="user_id")rename_conversation_response.raise_for_status()print('[rename result]')print(rename_conversation_response.json())