import os
import dashscope
# これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
dashscope.base_http_api_url = 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1'
response = dashscope.Generation.call(
api_key=os.getenv('DASHSCOPE_API_KEY'), # 環境変数を設定していない場合は、ここを API キーに置き換えてください
model='qwen-doc-turbo',
messages=[
{"role": "system","content": "You are a helpful assistant."},
{
"role": "user",
"content": [
{
"type": "text",
"text": "From these two product manuals, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed)."
},
{
"type": "doc_url",
"doc_url": [
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251107/jockge/%E7%A4%BA%E4%BE%8B%E4%BA%A7%E5%93%81%E6%89%8B%E5%86%8CA.docx",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251107/ztwxzr/%E7%A4%BA%E4%BE%8B%E4%BA%A7%E5%93%81%E6%89%8B%E5%86%8CB.docx"
],
"file_parsing_strategy": "auto"
}
]
}]
)
try:
if response.status_code == 200:
print(response.output.choices[0].message.content)
else:
print(f"Request failed, status code: {response.status_code}")
print(f"Error code: {response.code}")
print(f"Error message: {response.message}")
print("For more information, see https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
except Exception as e:
print(f"An error occurred: {e}")
print("For more information, see https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer $DASHSCOPE_API_KEY' \
--header 'X-DashScope-SSE: enable' \
--data '{
"model": "qwen-doc-turbo",
"input": {
"messages": [
{
"role": "system",
"content": "you are a helpful assistant."
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "From these two product manuals, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed)."
},
{
"type": "doc_url",
"doc_url": [
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251107/jockge/%E7%A4%BA%E4%BE%8B%E4%BA%A7%E5%93%81%E6%89%8B%E5%86%8CA.docx",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251107/ztwxzr/%E7%A4%BA%E4%BE%8B%E4%BA%A7%E5%93%81%E6%89%8B%E5%86%8CB.docx"
],
"file_parsing_strategy": "auto"
}
]
}
]
}
}'
import os
from openai import OpenAI, BadRequestError
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数を設定していない場合は、ここを API キーに置き換えてください
# これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
try:
completion = client.chat.completions.create(
model="qwen-doc-turbo",
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
# '{FILE_ID}' を実際のシナリオの file-id に置き換えてください
{'role': 'system', 'content': 'fileid://{FILE_ID}'},
{'role': 'user', 'content': 'From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed).'}
],
# このコード例では、モデルの出力プロセスを明確に示すためにストリーミング出力を使用しています。非ストリーミング出力の例については、https://www.alibabacloud.com/help/model-studio/user-guide/text-generation をご参照ください
stream=True,
stream_options={"include_usage": True}
)
full_content = ""
for chunk in completion:
if chunk.choices and chunk.choices[0].delta.content:
full_content += chunk.choices[0].delta.content
print(chunk.model_dump())
print(full_content)
except BadRequestError as e:
print(f"Error message: {e}")
print("For more information, see https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.models.chat.completions.*;
public class Main {
public static void main(String[] args) {
// クライアントを作成し、環境変数から API キーを使用します
OpenAIClient client = OpenAIOkHttpClient.builder()
// 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: .apiKey("sk-xxx");
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
.baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
.build();
ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
.addSystemMessage("You are a helpful assistant.")
// '{FILE_ID}' を実際のシナリオの file-id に置き換えてください
.addSystemMessage("fileid://{FILE_ID}")
.addUserMessage("From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed).")
.model("qwen-doc-turbo")
.build();
try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(chatParams)) {
streamResponse.stream().forEach(chunk -> {
String content = chunk.choices().get(0).delta().content().orElse("");
if (!content.isEmpty()) {
System.out.print(content);
}
});
} catch (Exception e) {
System.err.println("Error message: " + e.getMessage());
}
}
# これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
}
curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-doc-turbo",
"messages": [
{"role": "system","content": "You are a helpful assistant."},
{"role": "system","content": "fileid://{FILE_ID}"},
{"role": "user","content": "From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed)."}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
完全な例:ファイルのアップロードとモデルの呼び出し
import os
import time
from pathlib import Path
from openai import OpenAI, BadRequestError
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数を設定していない場合は、ここを API キーに置き換えてください
# これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
try:
# ステップ 1: ファイルのアップロード
file_object = client.files.create(file=Path("Sample Product Manual A.docx"), purpose="file-extract")
file_id = file_object.id
print(f"ファイルが正常にアップロードされました。file-id: {file_id}")
# ステップ 2: ファイルの解析が完了するのを待機します (オプション、大きなファイルの場合に必要になることがあります)
# ファイルがまだ解析中の場合、API はエラーを返します。リトライする必要があります。
max_retries = 10
retry_count = 0
while retry_count < max_retries:
try:
# ステップ 3: file-id を使用してモデルを呼び出す
completion = client.chat.completions.create(
model="qwen-doc-turbo",
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'system', 'content': f'fileid://{file_id}'},
{'role': 'user', 'content': 'From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed).'}
],
stream=True,
stream_options={"include_usage": True}
)
# ステップ 4: モデルの出力を処理する
full_content = ""
for chunk in completion:
if chunk.choices and chunk.choices[0].delta.content:
full_content += chunk.choices[0].delta.content
print(chunk.choices[0].delta.content, end='', flush=True)
print(f"\n\n完全な出力:\n{full_content}")
break
except BadRequestError as e:
if "File parsing in progress" in str(e):
retry_count += 1
print(f"ファイルは解析中です。遅延後にリトライします ({retry_count}/{max_retries})...")
time.sleep(2) # 2 秒待機してリトライします
else:
raise e
if retry_count >= max_retries:
print("ファイルの解析がタイムアウトしました。後でもう一度お試しください。")
except BadRequestError as e:
print(f"エラーメッセージ: {e}")
print("For more information, see https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
except Exception as e:
print(f"An error occurred: {e}")
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.models.chat.completions.*;
import com.openai.models.files.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
// クライアントを作成します
OpenAIClient client = OpenAIOkHttpClient.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
.baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
.build();
try {
// ステップ 1: ファイルのアップロード
Path filePath = Paths.get("src/main/java/org/example/Sample Product Manual A.docx");
FileCreateParams fileParams = FileCreateParams.builder()
.file(filePath)
.purpose(FilePurpose.of("file-extract"))
.build();
FileObject fileObject = client.files().create(fileParams);
String fileId = fileObject.id();
System.out.println("ファイルが正常にアップロードされました。file-id: " + fileId);
// ステップ 2: ファイルの解析を待ってからモデルを呼び出します (最大 10 回リトライ)
int maxRetries = 10;
int retryCount = 0;
boolean success = false;
while (retryCount < maxRetries && !success) {
try {
// ステップ 3: file-id を使用してモデルを呼び出す
ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
.addSystemMessage("You are a helpful assistant.")
.addSystemMessage("fileid://" + fileId)
.addUserMessage("From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed).")
.model("qwen-doc-turbo")
.build();
// ステップ 4: モデルの出力を処理する
try (StreamResponse<ChatCompletionChunk> streamResponse =
client.chat().completions().createStreaming(chatParams)) {
streamResponse.stream().forEach(chunk -> {
String content = chunk.choices().get(0).delta().content().orElse("");
if (!content.isEmpty()) {
System.out.print(content);
}
});
System.out.println();
success = true;
}
} catch (Exception e) {
if (e.getMessage() != null && e.getMessage().contains("File parsing in progress")) {
retryCount++;
System.out.println("ファイルは解析中です。遅延後にリトライします (" + retryCount + "/" + maxRetries + ")...");
TimeUnit.SECONDS.sleep(2); // 2 秒待機してリトライします
} else {
throw e;
}
}
}
if (!success) {
System.out.println("ファイルの解析がタイムアウトしました。後でもう一度お試しください。");
}
} catch (Exception e) {
System.err.println("エラーメッセージ: " + e.getMessage());
e.printStackTrace();
}
}
}
テキストコンテンツが 9,000 トークンを超える場合は、(API ボディサイズの制限により) 代わりにファイル URL またはファイル ID を使用してください。
import os
from openai import OpenAI, BadRequestError
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数を設定していない場合は、ここを API キーに置き換えてください
# これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
try:
completion = client.chat.completions.create(
model="qwen-doc-turbo",
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'system', 'content': 'Smart Office Product Manual Version: V2.0 Release Date: January 2024 Table of Contents 1.1 Product Overview...'},
{'role': 'user', 'content': 'From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed).'}
],
# このコード例では、モデルの出力プロセスを明確に示すためにストリーミング出力を使用しています。非ストリーミング出力の例については、https://www.alibabacloud.com/help/model-studio/user-guide/text-generation をご参照ください
stream=True,
stream_options={"include_usage": True}
)
full_content = ""
for chunk in completion:
if chunk.choices and chunk.choices[0].delta.content:
full_content += chunk.choices[0].delta.content
print(chunk.model_dump())
print(full_content)
except BadRequestError as e:
print(f"エラーメッセージ: {e}")
print("For more information, see https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.models.chat.completions.*;
public class Main {
public static void main(String[] args) {
// クライアントを作成し、環境変数から API キーを使用します
OpenAIClient client = OpenAIOkHttpClient.builder()
// 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: .apiKey("sk-xxx");
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
.baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
.build();
ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
.addSystemMessage("You are a helpful assistant.")
.addSystemMessage("Smart Office Product Manual Version: V2.0 Release Date: January 2024 Table of Contents 1.1 Product Overview...")
.addUserMessage("From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed).")
.model("qwen-doc-turbo")
.build();
try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(chatParams)) {
streamResponse.stream().forEach(chunk -> {
String content = chunk.choices().get(0).delta().content().orElse("");
if (!content.isEmpty()) {
System.out.print(content);
}
});
} catch (Exception e) {
System.err.println("Error message: " + e.getMessage());
}
}
# これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
}
curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-doc-turbo",
"messages": [
{"role": "system","content": "You are a helpful assistant."},
{"role": "system","content": "Smart Office Product Manual Version: V2.0 Release Date: January 2024 Table of Contents 1.1 Product Overview..."},
{"role": "user","content": "From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed)."}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
ファイル ID を使用して会話を開始してみてください。ファイルがまだ解析中の場合、API は File parsing in progress, please try again later. を返します。この場合は、少し時間をおいてからリトライしてください。呼び出しが成功した場合、ファイルの準備は完了です。
import os
import dashscope
# これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
dashscope.base_http_api_url = 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1'
response = dashscope.Generation.call(
api_key=os.getenv('DASHSCOPE_API_KEY'), # 環境変数を設定していない場合は、ここを API キーに置き換えてください
model='qwen-doc-turbo',
messages=[
{"role": "system","content": "You are a helpful assistant."},
{
"role": "user",
"content": [
{
"type": "text",
"text": "From these two product manuals, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed)."
},
{
"type": "doc_url",
"doc_url": [
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251107/jockge/%E7%A4%BA%E4%BE%8B%E4%BA%A7%E5%93%81%E6%89%8B%E5%86%8CA.docx",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251107/ztwxzr/%E7%A4%BA%E4%BE%8B%E4%BA%A7%E5%93%81%E6%89%8B%E5%86%8CB.docx"
],
"file_parsing_strategy": "auto"
}
]
}]
)
try:
if response.status_code == 200:
print(response.output.choices[0].message.content)
else:
print(f"Request failed, status code: {response.status_code}")
print(f"Error code: {response.code}")
print(f"Error message: {response.message}")
print("For more information, see https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
except Exception as e:
print(f"An error occurred: {e}")
print("For more information, see https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer $DASHSCOPE_API_KEY' \
--header 'X-DashScope-SSE: enable' \
--data '{
"model": "qwen-doc-turbo",
"input": {
"messages": [
{
"role": "system",
"content": "you are a helpful assistant."
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "From these two product manuals, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed)."
},
{
"type": "doc_url",
"doc_url": [
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251107/jockge/%E7%A4%BA%E4%BE%8B%E4%BA%A7%E5%93%81%E6%89%8B%E5%86%8CA.docx",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20251107/ztwxzr/%E7%A4%BA%E4%BE%8B%E4%BA%A7%E5%93%81%E6%89%8B%E5%86%8CB.docx"
],
"file_parsing_strategy": "auto"
}
]
}
]
}
}'
Details
ファイル ID を渡す方法
ファイルのアップロード
コードを実行する前に、サンプル製品マニュアル A をダウンロードし、プロジェクトディレクトリに配置します。OpenAI 互換インターフェイス経由でファイルをアップロードして file-id を取得します。アップロード API の詳細については、「API リファレンス」をご参照ください。
import os
from openai import OpenAI, BadRequestError
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数を設定していない場合は、ここを API キーに置き換えてください
# これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
try:
completion = client.chat.completions.create(
model="qwen-doc-turbo",
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
# '{FILE_ID}' を実際のシナリオの file-id に置き換えてください
{'role': 'system', 'content': 'fileid://{FILE_ID}'},
{'role': 'user', 'content': 'From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed).'}
],
# このコード例では、モデルの出力プロセスを明確に示すためにストリーミング出力を使用しています。非ストリーミング出力の例については、https://www.alibabacloud.com/help/model-studio/user-guide/text-generation をご参照ください
stream=True,
stream_options={"include_usage": True}
)
full_content = ""
for chunk in completion:
if chunk.choices and chunk.choices[0].delta.content:
full_content += chunk.choices[0].delta.content
print(chunk.model_dump())
print(full_content)
except BadRequestError as e:
print(f"Error message: {e}")
print("For more information, see https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.models.chat.completions.*;
public class Main {
public static void main(String[] args) {
// クライアントを作成し、環境変数から API キーを使用します
OpenAIClient client = OpenAIOkHttpClient.builder()
// 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: .apiKey("sk-xxx");
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
.baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
.build();
ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
.addSystemMessage("You are a helpful assistant.")
// '{FILE_ID}' を実際のシナリオの file-id に置き換えてください
.addSystemMessage("fileid://{FILE_ID}")
.addUserMessage("From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed).")
.model("qwen-doc-turbo")
.build();
try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(chatParams)) {
streamResponse.stream().forEach(chunk -> {
String content = chunk.choices().get(0).delta().content().orElse("");
if (!content.isEmpty()) {
System.out.print(content);
}
});
} catch (Exception e) {
System.err.println("Error message: " + e.getMessage());
}
}
# これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
}
curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-doc-turbo",
"messages": [
{"role": "system","content": "You are a helpful assistant."},
{"role": "system","content": "fileid://{FILE_ID}"},
{"role": "user","content": "From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed)."}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
テキストコンテンツが 9,000 トークンを超える場合は、(API ボディサイズの制限により) 代わりにファイル URL またはファイル ID を使用してください。
import os
from openai import OpenAI, BadRequestError
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数を設定していない場合は、ここを API キーに置き換えてください
# これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
)
try:
completion = client.chat.completions.create(
model="qwen-doc-turbo",
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'system', 'content': 'Smart Office Product Manual Version: V2.0 Release Date: January 2024 Table of Contents 1.1 Product Overview...'},
{'role': 'user', 'content': 'From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed).'}
],
# このコード例では、モデルの出力プロセスを明確に示すためにストリーミング出力を使用しています。非ストリーミング出力の例については、https://www.alibabacloud.com/help/model-studio/user-guide/text-generation をご参照ください
stream=True,
stream_options={"include_usage": True}
)
full_content = ""
for chunk in completion:
if chunk.choices and chunk.choices[0].delta.content:
full_content += chunk.choices[0].delta.content
print(chunk.model_dump())
print(full_content)
except BadRequestError as e:
print(f"エラーメッセージ: {e}")
print("For more information, see https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.models.chat.completions.*;
public class Main {
public static void main(String[] args) {
// クライアントを作成し、環境変数から API キーを使用します
OpenAIClient client = OpenAIOkHttpClient.builder()
// 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: .apiKey("sk-xxx");
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
.baseUrl("https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1")
.build();
ChatCompletionCreateParams chatParams = ChatCompletionCreateParams.builder()
.addSystemMessage("You are a helpful assistant.")
.addSystemMessage("Smart Office Product Manual Version: V2.0 Release Date: January 2024 Table of Contents 1.1 Product Overview...")
.addUserMessage("From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed).")
.model("qwen-doc-turbo")
.build();
try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(chatParams)) {
streamResponse.stream().forEach(chunk -> {
String content = chunk.choices().get(0).delta().content().orElse("");
if (!content.isEmpty()) {
System.out.print(content);
}
});
} catch (Exception e) {
System.err.println("Error message: " + e.getMessage());
}
}
# これは、中国北部 2 (北京) リージョンの URL です。リージョンごとに URL は異なります。
}
curl --location 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "qwen-doc-turbo",
"messages": [
{"role": "system","content": "You are a helpful assistant."},
{"role": "system","content": "Smart Office Product Manual Version: V2.0 Release Date: January 2024 Table of Contents 1.1 Product Overview..."},
{"role": "user","content": "From this product manual, extract all product information and organize it into a standard JSON array. Each object must include the following: model (the product model), name (the product name), and price (the price, with currency symbols and commas removed)."}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
ファイル ID を使用して会話を開始してみてください。ファイルがまだ解析中の場合、API は File parsing in progress, please try again later. を返します。この場合は、少し時間をおいてからリトライしてください。呼び出しが成功した場合、ファイルの準備は完了です。