langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
50.8k stars 7.3k forks source link

Refactor Python SDK and Construct dify_oapi to Improve Development Efficiency #10238

Open QiMington opened 2 days ago

QiMington commented 2 days ago

Self Checks

1. Is this request related to a challenge you're experiencing? Tell me about your story.

Is this request related to a challenge you are facing?

When I want to call the dify backend app from other backends, I find it difficult to know the types of request parameters and response parameters according to the original code logic, which makes it difficult to construct an interface request.

What is the feature you'd like to see?

The feature I would like to see is the corresponding request object and request body object being built through a builder, and the corresponding response data can be automatically structured into an object through pydantic.

How will this feature improve your workflow / experience?

After this, constructing request objects and reading response objects will become very easy and simple. The code hints from the editor will help me quickly build and read, thereby improving my development efficiency.

Can you help with this feature?

Yes, I have basically implemented this feature and am willing to submit my code.

2. Additional context or comments

A simple demo


from dify_oapi.api.chat.v1.model.chat_request import ChatRequest
from dify_oapi.api.chat.v1.model.chat_request_body import ChatRequestBody
from dify_oapi.api.chat.v1.model.chat_request_file import ChatRequestFile
from dify_oapi.client import Client
from dify_oapi.core.model.request_option import RequestOption

def main():
    client = Client.builder().domain("https://api.dify.ai").build()
    req_file = (
        ChatRequestFile.builder()
        .type("image")
        .transfer_method("remote_url")
        .url("https://cloud.dify.ai/logo/logo-site.png")
        .build()
    )
    req_body = (
        ChatRequestBody.builder()
        .inputs({})
        .query("What are the specs of the iPhone 13 Pro Max?")
        .response_mode("blocking")
        .conversation_id("")
        .user("abc-123")
        .files([req_file])
        .build()
    )
    req = ChatRequest.builder().request_body(req_body).build()
    req_option = RequestOption.builder().api_key("app-xxx").build()
    response = client.chat.v1.chat.chat(req, req_option, False)
    # response = await client.chat.v1.chat.achat(req, req_option, False)
    print(response.success)
    print(response.code)
    print(response.msg)
    print(response.answer)

if __name__ == "__main__":
    main()

3. Can you help us with this feature?

crazywoola commented 2 days ago

Please feel free to submit the SDK