### Install Qianfan Go SDK Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/go/how_to_use/plugin.md Install the SDK using the go get command. ```shell go get github.com/baidubce/bce-qianfan-sdk/go/qianfan ``` -------------------------------- ### Start Fine-tuning and Get Results Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/awesome_demo/dialogue_multi_tag_generation/main.ipynb Starts the fine-tuning process using the configured trainer and prints the training results. The `trainer.result` attribute holds the outcome of the training job. ```python trainer.start() print(trainer.result) ``` -------------------------------- ### Install datasets library Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/batch_prediction.ipynb Install the necessary library to handle HuggingFace datasets. ```bash !pip install datasets ``` -------------------------------- ### Start OpenAI Adapter Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/cli.md Starts a service simulating the OpenAI API interface for Qianfan models. ```console $ qianfan openai [OPTIONS] ``` ```shell export OPENAI_API_KEY='any-content-you-want' # 任意内容,仅为了绕过 OpenAI SDK 验证 export OPENAI_BASE_URL='http://127.0.0.1:8001/v1' # 模拟 OpenAI 接口的地址 ``` -------------------------------- ### Install Qianfan Python SDK and Guidance Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/use_guidance_with_qianfan.md Install the necessary libraries using pip. Ensure both 'qianfan' and 'guidance' are included in the command. ```shell pip install qianfan guidance ``` -------------------------------- ### Install Dependencies Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/RAG/pinecone/pinecone_qa.ipynb Install the necessary libraries for the Qianfan SDK, Pinecone, and LangChain. ```python %pip install -U pinecone-client==2.2.4 qianfan tiktoken langchain pymupdf -i https://pypi.tuna.tsinghua.edu.cn/simple/ ``` -------------------------------- ### Install Node.js SDK Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/javascript/README.md Install the SDK using npm or yarn. This is the first step to integrate with the Qianfan platform. ```bash npm install @baiducloud/qianfan ``` ```bash yarn add @baiducloud/qianfan ``` -------------------------------- ### Install Necessary Libraries Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/wandb/wandb.ipynb Install the required libraries: qianfan, wandb, and langchain. Ensure you are using compatible versions. ```python langchain 0.0.335 qianfan 0.1.3 wandb 0.16.0 ``` ```python !pip install qianfan !pip install wandb !pip install langchain ``` -------------------------------- ### Trainer Full Workflow Example Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/README.md This example demonstrates the complete workflow of using the Qianfan SDK's Trainer for fine-tuning. It covers loading a local dataset, uploading it to the Qianfan platform, fine-tuning an ERNIE-Speed-8K model, evaluating with Model, publishing the service, and finally calling the service. ```python import qianfan # Initialize the Qianfan client client = qianfan.Trainer() # Load local dataset and upload to Qianfan platform local_dataset_path = "./local_training_data.jsonl" qianfan_dataset = qianfan.Dataset.create(local_dataset_path) # Start fine-tuning job training_job = client.train( model="ERNIE-Speed-8K", dataset=qianfan_dataset, training_type="sft", epochs=3 ) # Evaluate the fine-tuned model evaluation_results = client.evaluate( model_id=training_job.result.job_id, dataset="./evaluation_data.jsonl" ) # Publish the model as a service service = client.publish( model_id=training_job.result.job_id ) # Call the deployed service response = client.inference( service_id=service.result.service_id, prompt="A beautiful landscape painting." ) print(f"Training Job ID: {training_job.result.job_id}") print(f"Evaluation Results: {evaluation_results}") print(f"Service ID: {service.result.service_id}") print(f"Inference Response: {response}") ``` -------------------------------- ### Install Baidu Cloud Qianfan SDK Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/javascript/console.md Install the SDK using npm or yarn. This is the first step before using any of the SDK's functionalities. ```bash npm install @baiducloud/qianfan # or yarn add @baiducloud/qianfan ``` -------------------------------- ### Install BOS SDK Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/finetune/api_based_finetune.ipynb Install the required Baidu Cloud BOS Python SDK. ```bash !pip install bce-python-sdk ``` -------------------------------- ### Install Qianfan SDK with Dataset Support Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/finetune/dpo_training.ipynb Install the Qianfan SDK with the necessary dependencies for dataset operations. This command ensures you have the 'dataset_base' extra installed. ```bash ! pip install "qianfan[dataset_base]" -U ``` -------------------------------- ### Start Fine-tuning Training Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/awesome_demo/essay_scoring/main.ipynb Start the fine-tuning training process. Use `trainer.start()` to run the training in a background process and persist task information locally for later viewing. Alternatively, `trainer.run()` blocks until the training is complete and streams logs. ```python trainer.start() ``` -------------------------------- ### Install Dependencies Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/extensions/semantic_kernel/rag_with_sk.ipynb Installs the necessary semantic-kernel and chromadb packages. Ensure you are using compatible versions. ```python ! pip install semantic-kernel==0.4.5.dev0 ! pip install chromadb==0.4.14 ``` -------------------------------- ### Search Memory Examples Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/extensions/semantic_kernel/rag_with_sk.ipynb Initiates examples for searching memory. This is a placeholder for memory search demonstration. ```python await search_memory_examples(kernel) ``` -------------------------------- ### CLI: OpenAI Adapter Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/cli.md Starts a local service that simulates the OpenAI API format to interface with Qianfan models. ```APIDOC ## CLI: qianfan openai ### Description Starts a service listening on a local port that translates OpenAI-formatted requests to Qianfan API calls. ### Options - **--host** (TEXT) - Optional - Host address (default: 0.0.0.0). - **--port** (INTEGER) - Optional - Port number (default: 8001). - **--config-file** (TEXT) - Optional - Path to YAML configuration file for model mapping. ``` -------------------------------- ### Install Qianfan SDK Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/python/README.pypi.md Use pip to install the package. Requires Python 3.7.0 or higher. ```bash pip install qianfan ``` -------------------------------- ### Install Qianfan Go SDK and Baidu BOS SDK Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/go/how_to_use/use_lite-v_model.md Install the necessary Go SDKs for Qianfan and Baidu BOS. Ensure your Go environment is set up correctly. ```shell go get github.com/baidubce/bce-qianfan-sdk/go/qianfan go get github.com/baidubce/bce-sdk-go/services/bos ``` -------------------------------- ### Start Training and Retrieve Output Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/awesome_demo/customer_service_conversation/main.ipynb Executes the training process and accesses the output results. ```python trainer5.start() print(trainer5.result) ``` ```python trainer5.output ``` -------------------------------- ### Install Dependencies Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/langchain_sequential.ipynb Installs the necessary Qianfan SDK and Langchain libraries. Ensure you are using a recent version of the Qianfan SDK. ```python #-# cell_skip ! pip install "qianfan>=0.2.2" -U ! pip install langchain ``` -------------------------------- ### Install Dependencies Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/extensions/semantic_kernel/agent_with_sk.ipynb Install the necessary Qianfan and Semantic Kernel libraries using pip. Ensure you are using compatible versions as the SDK is under active development. ```python #-# cell_skip ! pip install "qianfan>=0.3.0" -U ! pip install semantic-kernel=='0.4.5.dev0' ``` -------------------------------- ### Retrieve LLM Output Example Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/dataset/batch_inference_using_dataset.ipynb This example shows a typical output from an LLM API call, including the prompt, input prompt, LLM's generated output, expected output, and the request latency. ```json { "prompt": "地球的自转周期是多久?", "input_prompt": "地球的自转周期是多久?", "llm_output": "地球自转是地球绕自转轴自西向东的转动,从北极点上空看呈逆时针旋转,从南极点上空看呈顺时针旋转。地球自转轴与黄道面成66.34度夹角,与赤道面垂直。关于地球自转的各种理论目前都还是假说。地球自转是地球的一种重要运动形式,自转的平均角速度为4.167×10-3度/秒,在地球赤道上的自转线速度为465米/秒。而地球的自转周期是**一天**,即**24小时**。\n\n如需更多与地球自转有关的信息,建议阅读天文类书籍或请教天文学专业人士。", "expected_output": "大约24小时", "request_complete_latency": 0.00042041699998662807 } ``` -------------------------------- ### Initiate Trainer Task using CLI Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/cli.md Start a trainer task such as finetune, postpretrain, or dpo. Requires specifying the trainer pipeline file, training type, and dataset ID. ```bash $ qianfan trainer [finetune|postpretrain|dpo] [OPTIONS] ``` -------------------------------- ### Initialize SDK with AK/SK via Environment Variables Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/data.md Initialize the SDK by setting your Access Key and Secret Key as environment variables. Ensure you replace 'your_iam_ak' and 'your_iam_sk' with your actual credentials. ```python import os os.environ["QIANFAN_ACCESS_KEY"] = "your_iam_ak" os.environ["QIANFAN_SECRET_KEY"] = "your_iam_sk" ``` -------------------------------- ### Import Qianfan SDK Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/README.md Import the qianfan library into your Python project after installation to start using its functionalities. ```python import qianfan ``` -------------------------------- ### Get Bearer Token for V2 API Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/javascript/README.md Retrieves a bearer token required for authenticating V2 API calls. Ensure the SDK is installed and configured. ```typescript import {getBearToken} from "@baiducloud/qianfan"; const bearToken = await getBearToken(); console.log(bearToken); ``` -------------------------------- ### Install Qianfan SDK and Langchain Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/agents/langchain_agent_with_qianfan_llm.ipynb Install the necessary Python packages for Qianfan SDK and Langchain. It is recommended to upgrade if already installed. ```python #-# cell_skip !pip install qianfan !pip install langchain ``` ```python #-# cell_skip !pip install -U qianfan !pip install -U langchain ``` -------------------------------- ### Install Dependencies and Check Qianfan Version Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/agents/multi-agent-collaboration.ipynb Install necessary LangGraph, LangChain, and Qianfan dependencies. Verify the Qianfan installation by checking its version. ```python !pip install -U langchain langchain-community langchain_openai langgraph duckduckgo "qianfan[openai]" !qianfan --version ``` -------------------------------- ### Action Planner Example with Qianfan Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/extensions/semantic_kernel/agent_with_sk.ipynb Demonstrates using the ActionPlanner to find and execute a function for a given goal. It integrates Qianfan's chat completion service and imports several core Semantic Kernel skills. ```python # Copyright (c) Microsoft. All rights reserved. import semantic_kernel as sk from qianfan.extensions.semantic_kernel import QianfanChatCompletion from semantic_kernel.core_skills import FileIOSkill, MathSkill, TextSkill, TimeSkill from semantic_kernel.planning import ActionPlanner kernel = sk.Kernel() kernel.add_chat_service("erniebot", QianfanChatCompletion("ERNIE-Bot-4")) kernel.import_skill(MathSkill(), "math") kernel.import_skill(FileIOSkill(), "fileIO") kernel.import_skill(TimeSkill(), "time") kernel.import_skill(TextSkill(), "text") # create an instance of action planner. planner = ActionPlanner(kernel) # the ask for which the action planner is going to find a relevant function. ask = "What is the sum of 110 and 990?" # ask the action planner to identify a suitable function from the list of functions available. plan = await planner.create_plan_async(goal=ask) # ask the action planner to execute the identified function. result = await plan.invoke_async() print(result) """ Output: 1100 """ ``` -------------------------------- ### Initialize and Use Qianfan Plugins Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/javascript/browser.md Demonstrates how to initialize the Plugin client and invoke specific plugins like weather, image analysis, and knowledge bases. ```ts import {Plugin} from "@baiducloud/qianfan"; // 注意:千帆插件需要传入Endpoint, 一言插件不用 const client = new Plugin({QIANFAN_BASE_URL: 'http://172.18.184.85:8002', QIANFAN_CONSOLE_API_BASE_URL: 'http://172.18.184.85:8003', Endpoint: '***'}); // 天气插件 async function main() { const resp = await client.plugins({ query: '深圳今天天气如何', /** * 插件名称 * 知识库插件固定值为["uuid-zhishiku"] * 智慧图问插件固定值为["uuid-chatocr"] * 天气插件固定值为["uuid-weatherforecast"] */ plugins: [ 'uuid-weatherforecast', ], }); } // 智慧图问 async function chatocrMain() { const resp = await client.plugins({ query: '请解析这张图片, 告诉我怎么画这张图的简笔画', plugins: [ 'uuid-chatocr', ], fileurl: 'https://xxx.bcebos.com/xxx/xxx.jpeg', }); } // 知识库 async function zhishikuMain() { const reps = await client.plugins({ query: '你好什么时候飞行员需要负法律责任?', plugins: [ 'uuid-zhishiku', ], }); } main(); // chatocrMain(); // zhishikuMain(); ``` -------------------------------- ### Install Dependencies Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/sdxl_prompt_optimize/prompt_optimize.ipynb Install the required Python packages for the project. ```python !pip install -r requirement.txt ``` -------------------------------- ### Install Langchain, Qianfan SDK, and VectorDB Dependencies Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/RAG/vdb.ipynb Use this command to install the required Python packages for Langchain, Qianfan SDK, and VectorDB. Ensure you have pip installed. ```python ! pip install -U langchain qianfan pymochow ``` -------------------------------- ### Install Dependencies Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/text2image.ipynb Install the Pillow library required for image processing. ```python # 安装依赖 # install your dependency !pip install pillow ``` -------------------------------- ### Import Libraries and Setup Environment Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/sdxl_prompt_optimize/prompt_optimize.ipynb Import necessary modules and configure the environment for the Qianfan SDK and local tools. ```python import os,io,qianfan,torch,statistics import ae_score import matplotlib.pyplot as plt import numpy as np from PIL import Image import seaborn as sns from glob import glob from qianfan.common import Prompt from qianfan import Text2Image,Completion from tools import prompt_update from tools import t2i from tools import generate_and_save from transformers import AutoProcessor, AutoModel from matplotlib.ticker import MaxNLocator # 定义文件路径 infer_py_path = 'Pickscore-main/infer.py' raw_folder = 'raw' update_folder = 'update' # 将 infer.py 作为模块导入 import sys sys.path.append(os.path.dirname(infer_py_path)) from infer import calc_probs ``` -------------------------------- ### Install Qianfan Python SDK Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/README.md Install the Qianfan Python SDK using pip. Ensure Python version is 3.7.0 or higher. The '[dataset_base]' extra installs dataset-related functionalities. ```bash pip install 'qianfan[dataset_base]' ``` -------------------------------- ### Execute Post-Pretraining and Subsequent Fine-tuning Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/trainer.md Demonstrates a workflow involving a PostPreTrain task followed by an LLMFinetune task using the output of the previous trainer. ```python from qianfan.trainer import PostPreTrain, LLMFinetune from qianfan.trainer.configs import TrainConfig from qianfan.trainer.consts import PeftType from qianfan.dataset import Dataset # 泛文本 数据集 ds = Dataset.load(qianfan_dataset_id="ds-ag138") # postpretrain trainer = PostPreTrain( train_type="ERNIE-Speed", dataset=ds, ) trainer.run() # 这一步可以拿到训练完成的PostPretrain任务信息: print(trainer.output) # sft数据集 sft_ds = Dataset.load(qianfan_dataset_id="ds-47j7ztjxfz60wb8x") ppt_sft_trainer = LLMFinetune( train_type="ERNIE-Speed", dataset=sft_ds, train_config=TrainConfig( epoch=1, learning_rate=0.00003, max_seq_len=4096, peft_type=PeftType.ALL, ), name="qianfantrainer01" previous_trainer=trainer, ) ppt_sft_trainer.run() # 拿到最终的可用于推理部署的模型: print(ppt_sft_trainer.output) ``` -------------------------------- ### Install Deep Lake Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/RAG/deeplake_retrieval_qa.ipynb Install the required Deep Lake library version. ```text deeplake==3.8.6 ``` ```python !pip install deeplake ``` -------------------------------- ### Initialize Prompt for Optimization Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/sdxl_prompt_optimize/prompt_optimize.ipynb Create a prompt object from a simple text description. ```python pt = "夫妻肺片" #将文本转为LLM的prompt格式 p = Prompt(pt) ``` ```python pt = "一只高贵的柯基犬" #将文本转为LLM的prompt格式 p = Prompt(pt) ``` -------------------------------- ### Install Qianfan SDK Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/finetune/finetune_with_bos_and_evaluate.ipynb Install or upgrade the Qianfan SDK to version 0.3.0 or higher. ```python ! pip install "qianfan>=0.3.0" -U ``` -------------------------------- ### List Available Fine-tuning Models Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/awesome_demo/essay_scoring/main.ipynb Use the CLI to list available models for fine-tuning. ```bash !qianfan trainer finetune -l ``` -------------------------------- ### Text Completion Response Example Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/javascript/README.md This is an example of a typical non-streaming response from the text completion API. ```bash { id: 'as-hfmv5mvdim', object: 'chat.completion', created: 1709779789, result: '你好!请问有什么我可以帮助你的吗?无论你有什么问题或需要帮助,我都会尽力回答和提供支持。请随时告诉我你的需求,我会尽快回复你。', is_truncated: false, need_clear_history: false, finish_reason: 'normal', usage: { prompt_tokens: 1, completion_tokens: 34, total_tokens: 35 } } ``` -------------------------------- ### Initialize SDK with Environment Variables Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/javascript/README.md Configure SDK authentication by setting environment variables for Access Key (AK) and Secret Key (SK). This method is recommended for configuration via files or environment variables. ```bash QIANFAN_AK=your_access_key QIANFAN_SK=your_secret_key QIANFAN_ACCESS_KEY=another_access_key QIANFAN_SECRET_KEY=another_secret_key ``` ```javascript setEnvVariable('QIANFAN_AK','your_api_key'); setEnvVariable('QIANFAN_SK','your_secret_key'); ``` -------------------------------- ### Initiate Trainer Training via Command Line Tool Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/trainer.md Use the Qianfan SDK's command-line tool to initiate trainer fine-tuning. Specify the configuration file using the -f flag. Refer to the cli-trainer documentation for detailed command information. ```bash qianfan trainer finetune -f xx.json ``` -------------------------------- ### Install Required Dependencies Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/RAG/baidu_elasticsearch/qianfan_baidu_elasticsearch.ipynb Install the necessary Python packages for Langchain, Elasticsearch, Qianfan, and PDF processing. ```bash !pip install langchain==0.332 !pip install elasticsearch==7.11.0 !pip install qianfan !pip install pdfplumber ``` -------------------------------- ### Define Data Format Example Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/finetune/api_based_finetune.ipynb Example of the required JSONL format for LLM SFT training data. ```json [{"prompt" : "你好", "response": [["你需要什么帮助"]]}] ``` -------------------------------- ### Initialize LLMFinetune Trainer Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/finetune/finetune_with_bos_and_evaluate.ipynb Import the LLMFinetune trainer class to manage the SFT pipeline. ```python from qianfan.trainer import LLMFinetune # 首先需要先加载测试数据集,这里以加载bos上的数据集为例子: ``` -------------------------------- ### Initialize Completion Client (Default Model) Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/go/inference.md Initializes the Completion client using the default model. No specific model or endpoint is provided. ```go completion := qianfan.NewCompletion() ``` -------------------------------- ### Execute SequentialChain with Basic Input Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/langchain_sequential.ipynb Demonstrates running two chains in sequence where the output of the first is used by the second. ```python from langchain.chains import SequentialChain overall_chain = SequentialChain( chains=[synopsis_chain, review_chain], input_variables=["era", "title"], # Here we return multiple variables output_variables=["synopsis", "review"], verbose=True, ) overall_chain({"title": title, "era": era}) ``` -------------------------------- ### Install Qianfan SDK Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/finetune/trainer_finetune_event_resume.ipynb Install the Qianfan SDK using pip. Ensure you are using a version that supports trainer features. ```python ! pip install "qianfan>=0.2.2" -U ``` -------------------------------- ### Optimize Prompts with Datasets Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/prompt.ipynb Demonstrates using a dataset to perform prompt optimization via samples. ```python from qianfan.dataset import Dataset dataset = Dataset.load( data_file="./essay.jsonl", organize_data_as_group=False, input_columns=["question", "article"], reference_column="expect" ) old_prompt = Prompt(""" 请帮我批改如下高考作文,给出评价和评分(0-70分): 高考作文评分批改分为基础等级、发展等级、关于作文的其他项评定 1、基础等级 基础等级分内容和表达两项。 1)内容项 具体评分规则如下:符合题意、中心突出、内容充实、思想健康、感情真挚为一等,可按16-20分酌情给分;符合题意、主题明确、内容较充实、思想健康、感情真实为二等,可按11-15分酌情给分;基本符合题意、中心基本明确、内容单薄、思想基本健康、感情基本真实为三等,可按6-10分酌情给分;偏离题意、中心不明确、内容不当、思想不健康、感情虚假为四等,可按0-5分酌情给分。 2)表达项 具体评分规则如下:符合文体要求、结构严谨、语言流畅、字迹工整为一等,可按16-20分酌情给分;符合文体要求、结构完整、语言通顺、字迹清楚为二等,可按11-15分酌情给分;基本符合文体要求、结构基本完整、语言基本通顺、字迹基本清楚为三等,可按6-10分酌情给分;不符合文体要求、结构混乱、语言不通顺语病多、字迹潦草难辨为四等,可按0-5分酌情给分。 2、发展等级 基础等级分要与发展等级分相匹配,发展等级分不能跨越基础等级的得分等级。 具体评分规则如下:深刻、丰富、有文采、有创意为一等,可按16-20分酌情给分;较深刻、较丰富、较有文采、较有创意为二等,可按11-15分酌情给分;略显深刻、略显丰富、略显文采、略显创意为三等,可按6-10分酌情给分;个别语句有深意、个别例子较好、个别语句较精彩、个别地方有深意为四等,可按0-5分酌情给分。 3、关于作文的其他项评定 1)扣分项评定 出现错别字,1个错别字扣1分,重复不计,扣完5分为止;标点符号出现3处以上错误的酌情扣分;不足字数者,每少50字扣1分;无标题扣2分。 2)残篇评定 400字以上的文章,按评分标准评分,扣字数分。(少50个字扣1分) 400字以下的文章,20分以下评分,不再扣字数分。 200字以下的文章,10分以下评分,不再扣字数分。 只写一两句话的,给1分或2分,不评0分。 只写标题的,给1分或2分,不评0分。 完全空白的,评0分。 题目:{question} 作文内容:{article}""") new_prompt = old_prompt.apo_by_sample(example=dataset) ``` -------------------------------- ### Integrating with LangChain Agent Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/tool.md Provides a comprehensive example of integrating a custom `LightSwitchTool` with a LangChain Agent. It shows how to convert the tool, set up environment variables, initialize the LLM and Agent, and execute a task. ```python import os from langchain.agents import AgentExecutor from langchain_community.chat_models import QianfanChatEndpoint from qianfan.extensions.langchain.agents import QianfanSingleActionAgent os.environ["QIANFAN_AK"] = "此处填写你的AK" os.environ["QIANFAN_SK"] = "此处填写你的SK" # Assuming LightSwitchTool is defined as shown previously tools = [LightSwitchTool().to_langchain_tool()] llm = QianfanChatEndpoint(model="ERNIE-3.5-8K") agent = QianfanSingleActionAgent.from_system_prompt(tools, llm) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) print(agent_executor.run("帮我关闭电灯")) ``` -------------------------------- ### Initialize Completion Client (Specify Endpoint) Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/go/inference.md Initializes the Completion client and specifies a custom endpoint for the service. ```go completion := qianfan.NewCompletion( qianfan.WithEndpoint("your_custom_endpoint"), ) ``` -------------------------------- ### Install Qianfan SDK Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/finetune/trainer_finetune.ipynb Installs or upgrades the Qianfan Python SDK to version 0.3.15 or later. Ensure you have a compatible Python environment. ```python ! pip install "qianfan>=0.3.15" -U ``` -------------------------------- ### Initialize SDK with Parameters Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/javascript/README.md Initialize the ChatCompletion client by passing authentication parameters directly. This is useful for specific service configurations. ```javascript import {ChatCompletion} from "@baiducloud/qianfan"; // 通过参数初始化,应用API Key替换your_api_key,应用Secret Key替换your_secret_key,以对话Chat为例,调用如下 const client = new ChatCompletion({ QIANFAN_AK: 'your_api_key', QIANFAN_SK: 'your_secret_key' }); // 通过参数初始化, ACCESS_KEY / SECRET_KEY const client = new ChatCompletion({ QIANFAN_ACCESS_KEY: 'your_api_key', QIANFAN_SECRET_KEY: 'your_secret_key' }); ``` -------------------------------- ### Define and Initialize a LangChain Agent with Tools Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/wandb/wandb.ipynb Set up a LangChain Agent that uses QianfanChatEndpoint and includes a custom tool for getting weather information. This involves defining the LLM, prompt, tool schema, and agent logic. ```python from langchain.chat_models import QianfanChatEndpoint from langchain.agents import load_tools, initialize_agent, AgentType from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain.agents import tool from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser from langchain.agents.format_scratchpad import format_to_openai_functions llm = QianfanChatEndpoint(model="ERNIE-Bot") prompt = ChatPromptTemplate.from_messages([ ("system", "You are very powerful assistant, but bad at get today's temperature of location."), ("user", "{input}"), MessagesPlaceholder(variable_name="agent_scratchpad") ]) FUNCTION_SCHEMA_GET_WEATHER = { "name": "get_current_weather", "description": "获得指定地点的天气", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "省,市名,例如:河北省,石家庄" }, "unit": { "type": "string", "enum": ["摄氏度", "华氏度"] } }, "required": ["location"] } } @tool def get_current_weather(location: str) -> str: """Returns current temperature of location.""" return "25" tools = [get_current_weather] llm_with_tools = llm.bind( functions=[ FUNCTION_SCHEMA_GET_WEATHER, ] ) p = { "input": lambda x: x["input"], "agent_scratchpad": lambda x: format_to_openai_functions(x['intermediate_steps']), } agent = p | prompt | llm_with_tools | OpenAIFunctionsAgentOutputParser() ``` -------------------------------- ### Install Tabulate Package Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/essay_correction.ipynb Installs the 'tabulate' Python package, which is required for pretty-printing tables in the console. This command should be run in your terminal or a notebook cell. ```bash !pip install tabulate ``` -------------------------------- ### Example V2 API Response (Non-Streaming) Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/javascript/README.md An example response from the V2 API for a non-streaming chat completion, showing the assistant's reply. ```json [ { index: 0, message: { role: 'assistant', content: '很抱歉,我无法直接获取实时的天气信息。建议您通过以下几种方式查询今天深圳的天气情况:\n' + '\n' + '1. 天气预报应用:您可以在手机应用商店下载天气预报应用,然后搜索“深圳”以获取当地的天气情况。\n' + '\n' + '2. 搜索引擎:在搜索引擎中输入“深圳今天天气”或“深圳天气预报”,通常可以找到相关的天气信息。\n' + '\n' + '3. 社交媒体:有些社交媒体平台会提供天气预报功能,您可以关注相关的账号或搜索相关话题以获取天气信息。\n' + '\n' + '4. 电视或广播:如果您正在家中,可以打开电视或广播,通常会有天气预报节目或新闻播报天气情况。\n' + '\n' + '请注意,天气情况可能会随时变化,建议您在出门前再次确认天气情况,以便做好相应的准备。' }, finish_reason: 'normal', flag: 0 } ] ``` -------------------------------- ### Prepare Local Prompt for Upload Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/prompt.ipynb Initialize a local prompt with a template and a name, preparing it for upload to the Qianfan platform using `hub.push`. A name is mandatory for platform prompts. ```python p = Prompt( template="本地 prompt {var1}", # 对于平台上的 prompt 来说,name 是必须的,因此上传前必须先设置 name="cookbook_prompt" ) ``` -------------------------------- ### Example Response for Multi-turn Conversation Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/javascript/README.md This is an example JSON response structure for a multi-turn chat completion, detailing the conversation results and token usage. ```json { id: 'as-8vcq0n4u0e', object: 'chat.completion', created: 1709887877, result: '北京是一个拥有许多有趣和独特景点的大城市,周末你可以去很多地方玩。例如:\n' + '\n' + '1. **故宫博物院**:这是中国最大的古代建筑群,有着丰富的历史和文化遗产,是个很好的适合全家人游玩的地方。\n' + '2. **天安门广场**:这里是北京的心脏,周围有许多历史和现代建筑。你可以在广场上漫步,欣赏升旗仪式和观看周围的繁华景象。\n' + '3. **颐和园**:这是一个美丽的皇家园林,有着优美的湖泊和精美的古建筑。你可以在这里漫步,欣赏美丽的景色,同时也可以了解中国的传统文化。\n' + '4. **北京动物园**:这是中国最大的动物园之一,有许多稀有动物,包括熊猫、老虎、长颈鹿等。对于孩子们来说是个很好的去处。\n' + '5. **798艺术区**:这是一个充满艺术气息的地方,有许多画廊、艺术工作室和艺术展览。这里有许多新的现代艺术作品,可以欣赏到一些艺术家的创作。\n' + '6. **三里屯酒吧街**:如果你对夜生活感兴趣,可以去三里屯酒吧街。这里有许多酒吧和餐馆,是一个热闹的夜生活场所。\n' + '7. **北京环球度假区**:如果你们喜欢主题公园,那么可以去环球度假区,虽然这是在建的,但是等它建好之后肯定是一个很好的去处。\n' + '\n' + '当然,你也可以考虑一些其他的地方,比如购物街、博物馆、公园等等。希望这些建议对你有所帮助!', is_truncated: false, need_clear_history: false, usage: { prompt_tokens: 19, completion_tokens: 307, total_tokens: 326 } } ``` -------------------------------- ### Chat Completion Example Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/README.md Demonstrates how to use the ChatCompletion API to interact with a large language model. This example assumes authentication has already been configured. ```python import os import qianfan # 使用 Access Key 和 Secret Key 进行认证 os.environ["QIANFAN_ACCESS_KEY"] = "..." os.environ["QIANFAN_SECRET_KEY"] = "..." chat = qianfan.ChatCompletion() resp = chat.do(messages=[{"role": "user", "content": "你好,千帆"}]) print(resp["result"]) ``` -------------------------------- ### Apply Environment Variable Settings Before Use Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/configurable.md Demonstrates the correct order for setting environment variables for SDK configuration. Settings must be applied before the relevant SDK object is instantiated. ```python import os import qianfan os.environ["QIANFAN_QPS_LIMIT"] = "1" qianfan.ChatCompletion() ``` -------------------------------- ### Install Qianfan SDK Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/cookbook/evaluation/how_to_use_evaluation.ipynb Ensure you have Qianfan Python SDK version 0.3.0 or higher installed. This command upgrades the SDK if a lower version is present. ```python #-# cell_skip ! pip install -U "qianfan>=0.3.0" ``` -------------------------------- ### Initialize Qianfan Client Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/docs/go/how_to_use/plugin.md Create a new base model client instance. ```go qianfanClient := qianfan.NewBaseModel() ``` -------------------------------- ### Text to Embedding Response Example Source: https://github.com/baidubce/bce-qianfan-sdk/blob/main/javascript/README.md Example output showing the vector embeddings for input text. Each array represents the embedding for a corresponding input string. ```bash [0.06814255565404892, 0.007878394797444344, 0.060368239879608154, ...] [0.13463851809501648, -0.010635783895850182, 0.024348173290491104, ...] ```