### Python LangChain Integration Example Source: https://context7.com/aliyun/alibabacloud-bailian-sdk/llms.txt Shows how to integrate the Bailian SDK with LangChain for building complex conversational applications using Chains and Memory. Requires environment variables for credentials. ```python import os import uuid from langchain_bailian import Bailian from langchain.memory import ConversationBufferMemory from langchain.chains import ConversationChain # 初始化Bailian LLM llm = Bailian( access_key_id=os.environ.get("ACCESS_KEY_ID"), access_key_secret=os.environ.get("ACCESS_KEY_SECRET"), agent_key=os.environ.get("AGENT_KEY"), app_id=os.environ.get("APP_ID") ) # 简单调用 result = llm("1+1=?") print(f"Answer: {result}") # 使用ConversationChain进行多轮对话 memory = ConversationBufferMemory() conversation = ConversationChain( memory=memory, llm=llm, verbose=True ) response1 = conversation.run("我想明天去北京") print(f"Response 1: {response1}") response2 = conversation.run("那边有什么旅游景点吗") print(f"Response 2: {response2}") # 使用百炼平台的session_id管理上下文 session_id = str(uuid.uuid4()).replace('-', '') result1 = llm("我想明天去新疆", session_id=session_id) result2 = llm("那边有什么好玩的地方吗", session_id=session_id) print(f"With session: {result2}") ``` -------------------------------- ### Java SDK Stream Call Example Source: https://context7.com/aliyun/alibabacloud-bailian-sdk/llms.txt Demonstrates using the Java SDK with Reactor's Flux for reactive streaming output, suitable for real-time content display. Ensure environment variables for access keys and agent key are set. ```java import com.aliyun.broadscope.bailian.sdk.*; import com.aliyun.broadscope.bailian.sdk.models.*; import reactor.core.publisher.Flux; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; public class StreamExample { public static void main(String[] args) throws InterruptedException { String token = new AccessTokenClient( System.getenv("ACCESS_KEY_ID"), System.getenv("ACCESS_KEY_SECRET"), System.getenv("AGENT_KEY") ).getToken(); ApplicationClient client = ApplicationClient.builder() .token(token) .build(); List messages = new ArrayList<>(); messages.add(new ChatSystemMessage("你是一名天文学家")); messages.add(new ChatUserMessage("宇宙中为什么会存在黑洞")); CompletionsRequest request = new CompletionsRequest() .setAppId(System.getenv("APP_ID")) .setMessages(messages) .setParameters(new CompletionsRequest.Parameter() .setIncrementalOutput(true) .setResultFormat("message")); CountDownLatch latch = new CountDownLatch(1); Flux response = client.streamCompletions(request); response.subscribe( data -> { if (data.isSuccess()) { System.out.print(data.getData().getChoices().get(0).getMessage().getContent()); } }, err -> { System.err.println("Error: " + err.getMessage()); latch.countDown(); }, () -> { System.out.println("\nCompleted"); latch.countDown(); } ); latch.await(30, TimeUnit.SECONDS); } } ``` -------------------------------- ### Java SDK - ApplicationClient for Chat Completions Source: https://context7.com/aliyun/alibabacloud-bailian-sdk/llms.txt This Java SDK example shows how to use `ApplicationClient` for chat completions. It utilizes the Builder pattern for client instantiation and supports synchronous `completions()` calls. Connection pool options can be customized. ```java import com.aliyun.broadscope.bailian.sdk.*; import com.aliyun.broadscope.bailian.sdk.models.*; import java.util.ArrayList; import java.util.List; public class BaiLianExample { public static void main(String[] args) { String accessKeyId = System.getenv("ACCESS_KEY_ID"); String accessKeySecret = System.getenv("ACCESS_KEY_SECRET"); String agentKey = System.getenv("AGENT_KEY"); String appId = System.getenv("APP_ID"); // 创建Token客户端 AccessTokenClient accessTokenClient = new AccessTokenClient(accessKeyId, accessKeySecret, agentKey); String token = accessTokenClient.getToken(); // 配置连接选项 ConnectOptions connectOptions = new ConnectOptions(); connectOptions.setConnectPoolSize(20); connectOptions.setConnectTimeout(30000); connectOptions.setReadTimeout(60000); // 构建应用客户端 ApplicationClient client = ApplicationClient.builder() .token(token) .connectOptions(connectOptions) .build(); // 构建消息列表 List messages = new ArrayList<>(); messages.add(new ChatSystemMessage("你是一名历史学家")); messages.add(new ChatUserMessage("帮我生成一篇200字的文章,描述一下春秋战国的文化和经济")); // 构建请求 CompletionsRequest request = new CompletionsRequest() .setAppId(appId) .setMessages(messages) .setParameters(new CompletionsRequest.Parameter() .setResultFormat("message") .setTemperature(0.7) .setMaxTokens(500)); // 同步调用 CompletionsResponse response = client.completions(request); if (response.isSuccess()) { String content = response.getData().getChoices().get(0).getMessage().getContent(); System.out.println("Response: " + content); } else { System.out.printf("Error: %s - %s%n", response.getCode(), response.getMessage()); } } } ``` -------------------------------- ### Python Text Embeddings Example Source: https://context7.com/aliyun/alibabacloud-bailian-sdk/llms.txt Demonstrates calling the text embedding API using the Python SDK to convert text into vector representations for semantic search and similarity calculations. Ensure environment variables for credentials are set. ```python import os import broadscope_bailian from alibabacloud_bailian20230601.client import Client from alibabacloud_bailian20230601.models import CreateTextEmbeddingsRequest from alibabacloud_tea_openapi.models import Config access_key_id = os.environ.get("ACCESS_KEY_ID") access_key_secret = os.environ.get("ACCESS_KEY_SECRET") agent_key = os.environ.get("AGENT_KEY") # 配置客户端 config = Config( access_key_id=access_key_id, access_key_secret=access_key_secret, endpoint=broadscope_bailian.pop_endpoint # bailian.cn-beijing.aliyuncs.com ) client = Client(config=config) # 创建向量化请求 request = CreateTextEmbeddingsRequest( agent_key=agent_key, input=["今天天气怎么样", "我想去北京"], text_type="query" # query: 查询文本, document: 文档文本 ) response = client.create_text_embeddings(request=request) if response.status_code == 200 and response.body.success: for embedding in response.body.data.embeddings: print(f"Text index: {embedding.text_index}") print(f"Embedding dimension: {len(embedding.embedding)}") print(f"Embedding vector: {embedding.embedding[:5]}...") # 前5维 else: print(f"Error: {response.body.code} - {response.body.message}") ``` -------------------------------- ### Initialize and call Bailian LLM Source: https://github.com/aliyun/alibabacloud-bailian-sdk/blob/master/broadscope-bailian-langchain/README.md Demonstrates basic initialization of the Bailian LLM using environment variables and executing a simple query. ```python def test_bailian_llm(): """ 测试基础llm功能 """ access_key_id = os.environ.get("ACCESS_KEY_ID") access_key_secret = os.environ.get("ACCESS_KEY_SECRET") agent_key = os.environ.get("AGENT_KEY") app_id = os.environ.get("APP_ID") llm = Bailian(access_key_id=access_key_id, access_key_secret=access_key_secret, agent_key=agent_key, app_id=app_id) out = llm("1+1=?") print(out) ``` -------------------------------- ### Initialize AccessTokenClient Source: https://context7.com/aliyun/alibabacloud-bailian-sdk/llms.txt Initializes the AccessTokenClient for managing API access tokens. Ensure ACCESS_KEY_ID, ACCESS_KEY_SECRET, and AGENT_KEY environment variables are set. ```python import os import broadscope_bailian # Initialize access token client access_key_id = os.environ.get("ACCESS_KEY_ID") access_key_secret = os.environ.get("ACCESS_KEY_SECRET") agent_key = os.environ.get("AGENT_KEY") # Create client and get token client = broadscope_bailian.AccessTokenClient( access_key_id=access_key_id, access_key_secret=access_key_secret, agent_key=agent_key ) # Get token (auto-cached, auto-refreshed before expiration) token = client.get_token() print(f"Token: {token[:20]}...") # Or manually create token and get expiration time token, expired_time = client.create_token(agent_key=agent_key) print(f"Token expires at: {expired_time}") ``` -------------------------------- ### NL2SQL Application with Python SDK Source: https://context7.com/aliyun/alibabacloud-bailian-sdk/llms.txt This snippet demonstrates the Natural Language to SQL (NL2SQL) application. It converts natural language queries into SQL statements. Use `biz_params` to provide database table structure information and synonym definitions. ```python import os import broadscope_bailian client = broadscope_bailian.AccessTokenClient( access_key_id=os.environ.get("ACCESS_KEY_ID"), access_key_secret=os.environ.get("ACCESS_KEY_SECRET"), agent_key=os.environ.get("AGENT_KEY") ) token = client.get_token() # 定义数据库Schema信息 sql_schema = { "sqlInput": { "synonym_infos": "国民生产总值: GNP|Gross National Product", "schema_infos": [ { "columns": [ {"col_caption": "地区", "col_name": "region"}, {"col_caption": "年份", "col_name": "year"}, {"col_caption": "国民生产总值", "col_name": "gross_national_product"} ], "table_id": "t_gross_national_product_1", "table_desc": "国民生产总值表" } ] } } resp = broadscope_bailian.Completions(token=token).create( app_id=os.environ.get("APP_ID"), prompt="浙江近五年GNP总和是多少", biz_params=sql_schema ) if resp.get("Success"): print(f"Generated SQL: {resp.get('Data', {}).get('Text')}") # 输出示例: # SELECT SUM(gross_national_product) FROM t_gross_national_product_1 # WHERE region = '浙江' AND year >= YEAR(NOW()) - 5 ``` -------------------------------- ### Create Text Completion using Messages Format Source: https://context7.com/aliyun/alibabacloud-bailian-sdk/llms.txt Uses the Completions.create() API with the recommended messages format for text generation. Requires a valid token and APP_ID. Handles system and user roles for conversation. ```python import os import broadscope_bailian # Get token client = broadscope_bailian.AccessTokenClient( access_key_id=os.environ.get("ACCESS_KEY_ID"), access_key_secret=os.environ.get("ACCESS_KEY_SECRET"), agent_key=os.environ.get("AGENT_KEY") ) token = client.get_token() # Use messages format for calling (recommended) resp = broadscope_bailian.Completions(token=token).create( app_id=os.environ.get("APP_ID"), messages=[ {"role": "system", "content": "你是一名历史学家"}, {"role": "user", "content": "帮我生成一篇200字的文章,描述一下春秋战国的经济和文化"} ], result_format="message" ) if resp.get("Success"): content = resp.get("Data", {}).get("Choices", [])[0].get("Message", {}).get("Content") print(f"Response: {content}") else: print(f"Error: {resp.get('Code')} - {resp.get('Message')}") # Response example: # { # "Success": true, # "RequestId": "xxx", # "Data": { # "Choices": [{"Message": {"Role": "assistant", "Content": "春秋战国时期是中国历史上..."}}], # "Usage": [{"InputTokens": 50, "OutputTokens": 200}] # } # } ``` -------------------------------- ### Streaming Text Generation with Tongyi Bailian SDK Source: https://github.com/aliyun/alibabacloud-bailian-sdk/blob/master/broadscope-bailian-sdk-python/README.md This snippet demonstrates how to receive text generation responses in a streaming fashion. It also requires token-based authentication and includes logic for handling the streamed output. ```python access_key_id = "******" access_key_secret = "******" agent_key = "******" client = AccessTokenClient(access_key_id=access_key_id, access_key_secret=access_key_secret) token, expired_time = client.create_token(agent_key=agent_key) # TODO 应用侧需要自己缓存token和过期时间, 过期前重新生成 broadscope_bailian.api_key = token app_id = "******" prompt = "帮我查询酒店" resp = broadscope_bailian.Completions().call(app_id=app_id, prompt=prompt, stream=True, has_thoughts=True) for line in resp: now = datetime.now() print("%s: %s" % (now, line), end="\n", flush=True) ``` -------------------------------- ### Completions.create() - Text Generation Source: https://context7.com/aliyun/alibabacloud-bailian-sdk/llms.txt Core API for generating text responses using various models, supporting both single-turn and multi-turn conversations. ```APIDOC ## POST Completions.create() ### Description Generates text based on provided messages or prompts. Supports system prompts, conversation history, and model parameter tuning. ### Parameters #### Request Body - **app_id** (string) - Required - The application ID - **messages** (array) - Required - List of message objects with 'role' and 'content' - **result_format** (string) - Optional - Format of the result (e.g., 'message') - **top_p** (float) - Optional - Nucleus sampling probability - **top_k** (integer) - Optional - Number of tokens to consider for sampling - **temperature** (float) - Optional - Sampling temperature - **max_tokens** (integer) - Optional - Maximum tokens to generate - **seed** (integer) - Optional - Random seed for reproducibility - **session_id** (string) - Optional - ID for maintaining conversation context - **stream** (boolean) - Optional - Enable streaming output - **incremental_output** (boolean) - Optional - Enable incremental output for streaming ### Response #### Success Response (200) - **Success** (boolean) - Indicates if the request was successful - **Data** (object) - Contains 'Choices' and 'Usage' information ### Response Example { "Success": true, "RequestId": "xxx", "Data": { "Choices": [{"Message": {"Role": "assistant", "Content": "..."}}], "Usage": [{"InputTokens": 50, "OutputTokens": 200}] } } ``` -------------------------------- ### AccessTokenClient - Authentication Source: https://context7.com/aliyun/alibabacloud-bailian-sdk/llms.txt Manages the creation and caching of API access tokens using Alibaba Cloud AccessKey credentials. ```APIDOC ## AccessTokenClient ### Description Handles authentication and token management for the BaiLian API. It automatically manages token caching and refreshing. ### Parameters #### Request Body - **access_key_id** (string) - Required - Alibaba Cloud Access Key ID - **access_key_secret** (string) - Required - Alibaba Cloud Access Key Secret - **agent_key** (string) - Required - Application Agent Key ``` -------------------------------- ### RAG Application with Python SDK Source: https://context7.com/aliyun/alibabacloud-bailian-sdk/llms.txt Use this snippet for Retrieval Augmented Generation (RAG) applications. It allows combining knowledge base documents with prompts for question answering. Configure `doc_reference_type` for document reference information and `doc_tag_codes` to specify documents for retrieval. ```python import os import broadscope_bailian client = broadscope_bailian.AccessTokenClient( access_key_id=os.environ.get("ACCESS_KEY_ID"), access_key_secret=os.environ.get("ACCESS_KEY_SECRET"), agent_key=os.environ.get("AGENT_KEY") ) token = client.get_token() # 设置对话历史 chat_history = [ {"user": "API接口如何使用", "bot": "API接口需要传入prompt、app id并通过post方法调用"} ] resp = broadscope_bailian.Completions(token=token).create( app_id=os.environ.get("APP_ID"), prompt="API接口说明中, TopP参数改如何传递?", history=chat_history, doc_reference_type="simple", # simple: 基本信息, indexed: 包含索引位置 doc_tag_codes=["471d*******3427", "881f*****0c232"] # 文档标签 ) if resp.get("Success"): print(f"Answer: {resp.get('Data', {}).get('Text')}") # 获取文档引用 doc_refs = resp.get("Data", {}).get("DocReferences", []) if doc_refs: for ref in doc_refs: print(f"Referenced doc: {ref.get('DocName')}") else: print(f"Error: {resp.get('Code')} - {resp.get('Message')}") ``` -------------------------------- ### Text Completion with Advanced Parameters and Session ID Source: https://context7.com/aliyun/alibabacloud-bailian-sdk/llms.txt Fine-tunes model output using parameters like top_p, top_k, temperature, and max_tokens. Supports automatic context maintenance via session_id or manual history management with messages. ```python import os import uuid import broadscope_bailian client = broadscope_bailian.AccessTokenClient( access_key_id=os.environ.get("ACCESS_KEY_ID"), access_key_secret=os.environ.get("ACCESS_KEY_SECRET"), agent_key=os.environ.get("AGENT_KEY") ) token = client.get_token() # Use session_id for automatic context maintenance session_id = str(uuid.uuid4()).replace("-", "") # Set multi-turn conversation history messages = [ {"role": "system", "content": "你是一个旅行专家, 能够帮我们制定旅行计划"}, {"role": "user", "content": "我想去北京"}, {"role": "assistant", "content": "北京是一个非常值得去的地方"}, {"role": "user", "content": "那边有什么推荐的旅游景点"} ] resp = broadscope_bailian.Completions(token=token).create( app_id=os.environ.get("APP_ID"), messages=messages, session_id=session_id, top_p=0.2, # Control output diversity, between 0-1 top_k=50, # Number of tokens to consider for sampling seed=2222, # Random seed for reproducible results temperature=0.3, # Sampling temperature, lower is more deterministic max_tokens=50, # Maximum output tokens result_format="message", stop=["景点"], # Stop words list timeout=30 # Timeout in seconds ) if resp.get("Success"): content = resp.get("Data", {}).get("Choices", [])[0].get("Message", {}).get("Content") usage = resp.get("Data", {}).get("Usage", [{}])[0] print(f"Response: {content}") print(f"Input tokens: {usage.get('InputTokens')}, Output tokens: {usage.get('OutputTokens')}") ``` -------------------------------- ### Text Generation with Tongyi Bailian SDK Source: https://github.com/aliyun/alibabacloud-bailian-sdk/blob/master/broadscope-bailian-sdk-python/README.md Use this snippet to generate text responses from the Tongyi Bailian service. It requires authentication using an access token, which should be cached and refreshed before expiration. ```python def test_completions(): access_key_id = "******" access_key_secret = "******" agent_key = "******" client = AccessTokenClient(access_key_id=access_key_id, access_key_secret=access_key_secret) token, expired_time = client.create_token(agent_key=agent_key) # TODO 应用侧需要自己缓存token和过期时间, 过期前重新生成 broadscope_bailian.api_key = token app_id = "******" prompt = "帮我查询下酒店" resp = broadscope_bailian.Completions().call(app_id=app_id, prompt=prompt) print(resp) ``` -------------------------------- ### Stream Text Generation Source: https://context7.com/aliyun/alibabacloud-bailian-sdk/llms.txt Enables streaming output for long text generation scenarios, improving user experience with incremental content delivery. Requires setting stream=True and incremental_output=True. ```python import os import broadscope_bailian client = broadscope_bailian.AccessTokenClient( access_key_id=os.environ.get("ACCESS_KEY_ID"), access_key_secret=os.environ.get("ACCESS_KEY_SECRET"), agent_key=os.environ.get("AGENT_KEY") ) token = client.get_token() resp = broadscope_bailian.Completions(token=token).create( app_id=os.environ.get("APP_ID"), messages=[ {"role": "system", "content": "你是一名天文学家, 能够帮助小学生回答宇宙与天文方面的问题"}, {"role": "user", "content": "宇宙中为什么会存在黑洞"} ], stream=True, result_format="message", incremental_output=True # Incremental output, does not include already output content ) # Stream processing of response for result in resp: if result.get("Success"): content = result.get("Data", {}).get("Choices", [])[0].get("Message", {}).get("Content") print(content, end="", flush=True) else: print(f"\nError: {result.get('Code')} - {result.get('Message')}") # Output example (character by character): # 黑洞是宇宙中一种非常神秘的天体... ``` -------------------------------- ### Implement ConversationChain with Memory Source: https://github.com/aliyun/alibabacloud-bailian-sdk/blob/master/broadscope-bailian-langchain/README.md Uses ConversationBufferMemory to maintain context across multiple turns in a conversation chain. ```python def test_conversation_chain(): """ 测试包含memory的chain """ access_key_id = os.environ.get("ACCESS_KEY_ID") access_key_secret = os.environ.get("ACCESS_KEY_SECRET") agent_key = os.environ.get("AGENT_KEY") app_id = os.environ.get("APP_ID") llm = Bailian(access_key_id=access_key_id, access_key_secret=access_key_secret, agent_key=agent_key, app_id=app_id) memory = ConversationBufferMemory() conversant_chain = ConversationChain(memory=memory, llm=llm, verbose=True) conversant_chain.run("我想明天去北京") out = conversant_chain.run("那边有什么旅游景点吗") print(out) ``` ```python def test_conversant_memory(): """ 测试包含memory的conversant chain """ access_key_id = os.environ.get("ACCESS_KEY_ID") access_key_secret = os.environ.get("ACCESS_KEY_SECRET") agent_key = os.environ.get("AGENT_KEY") app_id = os.environ.get("APP_ID") llm = Bailian(access_key_id=access_key_id, access_key_secret=access_key_secret, agent_key=agent_key, app_id=app_id) memory = ConversationBufferMemory() conversant_chain = ConversationChain(memory=memory, llm=llm, verbose=True) conversant_chain.run("我想明天去北京") out = conversant_chain.run("那边有什么旅游景点吗") print(out) ``` -------------------------------- ### Manage session context with Bailian Source: https://github.com/aliyun/alibabacloud-bailian-sdk/blob/master/broadscope-bailian-langchain/README.md Uses a session ID to maintain conversation context directly through the Bailian LLM interface. ```python def test_conversant_bailian_session(): """ 测试基于broadscope bailian的session id保存上下文 """ access_key_id = os.environ.get("ACCESS_KEY_ID") access_key_secret = os.environ.get("ACCESS_KEY_SECRET") agent_key = os.environ.get("AGENT_KEY") app_id = os.environ.get("APP_ID") llm = Bailian(access_key_id=access_key_id, access_key_secret=access_key_secret, agent_key=agent_key, app_id=app_id) session_id = str(uuid.uuid4()).replace('-', '') out = llm("我想明天去新疆", session_id=session_id) print(out) out = llm("那边有什么好玩的地方吗", session_id=session_id) print(out) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.