This topic describes how to call an application created in Alibaba Cloud Model Studio by using the SDK.
Prerequisites
Alibaba Cloud Model Studio is activated. For more information, see Activate Alibaba Cloud Model Studio.
An API key is obtained. For more information, see Obtain an API key.
The latest version of the SDK is installed. For more information, see Install Alibaba Cloud Model Studio SDK.
An application is created and its ID is obtained. For more information, see Application center and Obtain an application ID and a workspace ID.
Sample code
The following samples show how to call an application.
Replace YOUR_API_KEY with your API key and YOUR_APP_ID with your application ID.
If you use Python, the SDK version must be 1.10.0 or later.
If you use Java, the SDK version must be 2.5.0 or later.
Specify API key
export DASHSCOPE_API_KEY=YOUR_API_KEY
Call an application
from http import HTTPStatus
from dashscope import Application
def app_call():
response = Application.call(app_id='YOUR_APP_ID',
prompt='How do you specify the Top P parameter when you call an API operation?',
)
if response.status_code != HTTPStatus.OK:
print('request_id=%s, code=%s, message=%s\n' % (response.request_id, response.status_code, response.message))
else:
print('request_id=%s\n output=%s\n usage=%s\n' % (response.request_id, response.output, response.usage))
if __name__ == '__main__':
app_call()
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import java.util.List;
public class Main{
public static void appCall()
throws ApiException, NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
.appId("YOUR_APP_ID")
.prompt("'How do you specify the Top P parameter when you call an API operation?")
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());
if (result.getUsage() != null && result.getUsage().getModels() != null) {
for (ApplicationUsage.ModelUsage usage : result.getUsage().getModels()) {
System.out.printf("modelId: %s, inputTokens: %d, outputTokens: %d\n",
usage.getModelId(), usage.getInputTokens(), usage.getOutputTokens());
}
}
}
public static void main(String[] args) {
try {
appCall();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
}
System.exit(0);
}
}
Sample response
request_id=131c75de-b53d-9787-a301-1a841dc73eda
output={"text": "In application calls, Top P is an optional request parameter. It is used to control the probability threshold for nucleus sampling during output generation. You can specify the Top P value in the API request body following the JSON format: \n\n```json\n{\n \"TopP\": <float_value>\n}\n```\n\n. Replace <float_value> with a desired floating-point number in the range from 0.0 to 1.0. Top P can control the number of sampled tokens, affecting the randomness and variability of the output. When ordered from most-to-least probable, the most probable tokens are sampled if their probabilities add up to reach or exceed the threshold specified by the Top P value. A higher Top P value means greater variability.", "finish_reason": "stop", "session_id": "25d6b00b574a4740b6e8a97500b556d5", "thoughts": null, "doc_references": null}
usage={"models": [{"model_id": "qwen-max", "input_tokens": 1814, "output_tokens": 165}]}
References
For more information about how to call an application by using SDK, see SDK reference.