Add python code snippet for developing with apis (#95)

* Add python code snippet for developing with apis

* Update developing-with-apis.md

* Add python code snippet to EN develop with apis
pull/97/head
Mark Sun 2024-05-21 13:07:51 +08:00 committed by GitHub
parent 43f0333012
commit a0479e33a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 114 additions and 3 deletions

View File

@ -33,6 +33,8 @@ You can find the API documentation and example requests for this application in
For example, here is a sample call an API for text generation:
{% tabs %}
{% tab title="cURL" %}
```
curl --location --request POST 'https://api.dify.ai/v1/completion-messages' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
@ -43,6 +45,32 @@ curl --location --request POST 'https://api.dify.ai/v1/completion-messages' \
"user": "abc-123"
}'
```
{% endtab %}
{% tab title="Python" %}
```python
import requests
import json
url = "https://api.dify.ai/v1/completion-messages"
headers = {
'Authorization': 'Bearer ENTER-YOUR-SECRET-KEY',
'Content-Type': 'application/json',
}
data = {
"inputs": {"text": 'Hello, how are you?'},
"response_mode": "streaming",
"user": "abc-123"
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.text)
```
{% endtab %}
{% endtabs %}
### Conversational applications
@ -52,6 +80,8 @@ You can find the API documentation and example requests for this application in
For example, here is a sample call an API for chat-messages:
{% tabs %}
{% tab title="cURL" %}
```
curl --location --request POST 'https://api.dify.ai/v1/chat-messages' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
@ -63,4 +93,31 @@ curl --location --request POST 'https://api.dify.ai/v1/chat-messages' \
"conversation_id": "1c7e55fb-1ba2-4e10-81b5-30addcea2276"
"user": "abc-123"
}'
```
{% endtab %}
{% tab title="Python" %}
```python
import requests
import json
url = 'https://api.dify.ai/v1/chat-messages'
headers = {
'Authorization': 'Bearer ENTER-YOUR-SECRET-KEY',
'Content-Type': 'application/json',
}
data = {
"inputs": {},
"query": "eh",
"response_mode": "streaming",
"conversation_id": "1c7e55fb-1ba2-4e10-81b5-30addcea2276",
"user": "abc-123"
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.json())
```
{% endtab %}
{% endtabs %}

View File

@ -32,7 +32,8 @@ Dify 基于“**后端即服务**”理念为所有应用提供了 API为 AI
你可以在**应用 -> 访问 API** 中找到该应用的 API 文档与范例请求。
例如,创建文本补全信息的 API 的调用示例:
{% tabs %}
{% tab title="cURL" %}
```
curl --location --request POST 'https://api.dify.ai/v1/completion-messages' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
@ -42,8 +43,34 @@ curl --location --request POST 'https://api.dify.ai/v1/completion-messages' \
"response_mode": "streaming",
"user": "abc-123"
}'
```
{% endtab %}
{% tab title="Python" %}
```python
import requests
import json
url = "https://api.dify.ai/v1/completion-messages"
headers = {
'Authorization': 'Bearer ENTER-YOUR-SECRET-KEY',
'Content-Type': 'application/json',
}
data = {
"inputs": {"text": 'Hello, how are you?'},
"response_mode": "streaming",
"user": "abc-123"
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.text)
```
{% endtab %}
{% endtabs %}
### 对话型应用
@ -52,7 +79,8 @@ curl --location --request POST 'https://api.dify.ai/v1/completion-messages' \
你可以在**应用 -> 访问 API** 中找到该应用的 API 文档与范例请求。
例如,发送对话信息的 API的调用示例
{% tabs %}
{% tab title="cURL" %}
```
curl --location --request POST 'https://api.dify.ai/v1/chat-messages' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
@ -66,3 +94,29 @@ curl --location --request POST 'https://api.dify.ai/v1/chat-messages' \
}'
```
{% endtab %}
{% tab title="Python" %}
```python
import requests
import json
url = 'https://api.dify.ai/v1/chat-messages'
headers = {
'Authorization': 'Bearer ENTER-YOUR-SECRET-KEY',
'Content-Type': 'application/json',
}
data = {
"inputs": {},
"query": "eh",
"response_mode": "streaming",
"conversation_id": "1c7e55fb-1ba2-4e10-81b5-30addcea2276",
"user": "abc-123"
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.json())
```
{% endtab %}
{% endtabs %}