ERNIE SDK

repository·develop·Indexed 18 days ago

https://github.com/paddlepaddle/ernie-sdk

A toolkit for interacting with Baidu's Wenxin large models. It includes 'ERNIE Bot' for text, image, and vector access (supporting models like ernie-3.5, ernie-4.0, and ernie-vilg-v2), and 'ERNIE Bot Agent' for building AI agents with tool-calling and orchestration. The SDK provides a Python API and CLI for chat completion, text embeddings, and image generation, as well as specialized applications like ERNIEBot Researcher and a Function Calling Demo.

Tokens
70.4K
Snippets
189
Records
255
Agent score
63%

What's inside ernie-sdk

  1. Overview of ERNIE SDK components

    develop

    The ERNIE SDK consists of two primary projects:

    1. ERNIE Bot Agent: An agent development framework based on the orchestration capabilities of the Wenxin large model. It allows for multi-tool orchestration and automatic scheduling using Function Calling. It features a rich component library including pre-set tools (from the Xinghe Community), knowledge bases (supporting platform-based or open-source libraries like langchain and llama_index), and future plugin support.
    2. ERNIE Bot: The underlying dependency for the Agent. It provides direct interfaces for core Wenxin model capabilities such as text creation, general conversation, semantic vectors, and AI image generation.

    Developers can use the Agent for complex task orchestration or use ERNIE Bot directly for basic LLM tasks.

  2. Overview of ERNIE SDK

    develop

    The ERNIE SDK repository consists of two primary projects:

    1. ERNIE Bot Agent: An agent development framework based on Baidu's Wenxin large model orchestration capabilities. It allows for the orchestration of tools, plugins, and knowledge bases using Function Calling for automatic scheduling.
    2. ERNIE Bot: A foundational library providing easy-to-use interfaces for calling Wenxin large model capabilities, including text creation, general conversation, semantic vectors, and AI image generation.

    Supported Python versions: 3.8+ Supported OS: Linux, Windows, macOS.

  3. Overview of the erniebot_agent.tools module

    develop
    The erniebot_agent.tools module provides the infrastructure for extending the capabilities of an ERNIE Bot Agent through various tool types. It includes base classes for defining custom tools, mechanisms for managing tools, and support for remote tools. The module also provides several built-in tools for common tasks like searching, calculation, and time retrieval.
  4. Explore ERNIE Bot features in the Quick Start demo

    develop

    The Quick Start demo provides a visual interface to test three core capabilities of ERNIE Bot. Note that most features require authentication parameters (API keys/secrets) obtained from the official authentication documentation.

    1. Chat Completion

    Tests generative dialogue capabilities using models like ernie-3.5 and ernie-3.5-turbo. Enter your authentication parameters in the sidebar, input your text, and submit.

    2. Embedding

    Generates semantic vector representations for input text and calculates the cosine similarity between two different vectors.

    3. Image Generation

    Generates images of various sizes based on text prompts. This requires an API key and secret key specifically from the Intelligent Creative Platform (智能创作平台).

  5. Explore ERNIE Bot demonstration applications and code examples

    develop

    The examples/ directory contains various demonstration applications and code samples built on top of ERNIE Bot. These are categorized into three main types of functional demonstrations:

    1. Basic Functionality Demos: Covers core capabilities including dialogue completion (chat), semantic vectors (embeddings), and text-to-image generation.
    2. Function Calling Demos: Demonstrates how the model can request to execute functions (either pre-provided or user-defined) during a conversation. This is useful for debugging local code and optimizing function calling performance.
    3. RAG and Function Calling Demos: Shows how to combine Retrieval-Augmented Generation (RAG) with function calling to extend the model's knowledge into proprietary or specialized domains.
  6. Interface overview of the Function Calling Demo

    develop

    The demo application interface is divided into four functional zones:

    • Parameter Configuration Area (参数配置区): Set backend types, authentication parameters, and model types. Use 'Advanced Configuration' to adjust top_p and temperature.
    • Conversation Area (对话区): The main chat interface. Supports sending messages, resetting conversations, withdrawing messages, and regenerating the latest response.
    • Function Calling Area (函数调用区):
      • Left side: View pre-defined function definitions and descriptions.
      • Right side: Execute specific function calls by providing the name and request parameters. Includes a mechanism to send call results back to the model.
      • Selection: Checkbox group to select which functions are available for the model to reference.
    • Log Area (日志区): Displays the raw data for the last 10 messages, useful for debugging.
  7. Supported Backend Platforms and Models

    develop

    ERNIE Bot supports multiple backend platforms, each with different api_type values and supported models. Choose the backend that matches your credentials and required model:

    Backendapi_typeSupported Models
    AI Studioaistudioernie-3.5, ernie-turbo, ernie-4.0, ernie-3.5-8k, ernie-text-embedding
    Qianfan (千帆)qianfanernie-3.5, ernie-turbo, ernie-4.0, ernie-3.5-8k, ernie-text-embedding
    Intelligent Creation Platform (智能创作平台)yinianernie-vilg-v2
  8. What is the Retrieval module?

    develop

    The Retrieval module is an interface designed to return relevant documents when receiving unstructured queries. It supports both keyword-based retrieval and semantic retrieval (using high-dimensional vector representations to capture user intent).

    Key characteristics:

    • No storage required: The component focuses on retrieving or returning documents rather than storing them.
    • Database operations: It can flexibly perform add, delete, find, and modify operations on upstream databases.
    • RAG & Agent integration: It is a core component for Retrieval-Augmented Generation (RAG) and serves as a foundational tool for Agents, allowing users to mount private documents as external knowledge.
    • Compatibility: It supports Baizhong Search and is compatible with LangChain and LlamaIndex retrieval components.
  9. Understand the erniebot.EmbeddingResponse object

    develop

    The erniebot.Embedding.create method returns an erniebot.EmbeddingResponse object. You can access data using dictionary-style access resp["data"] or attribute-style access resp.data.

    Key Fields in the Response:

    • rcode: The HTTP response status code.
    • data: A list of dictionaries containing the embeddings. Each dictionary includes:
      • object: Fixed as "embedding".
      • embedding: The generated vector (for ernie-text-embedding, the dimension is 384).
      • index: The index of the input text.
    • usage: Token usage statistics:
      • prompt_tokens: Number of input tokens.
      • total_tokens: Total number of input and output tokens.

    Helper Method:

    • resp.get_result(): Returns a Python list containing only the vector results for each input text in order.
  10. Understand token limits and context window behavior

    develop

    The models have specific limits on the number of input tokens allowed:

    ModelToken Limit
    ernie-3.5, ernie-turbo, ernie-4.03000
    ernie-3.5-8k7000

    Behavior in different scenarios:

    • Single-turn dialogue: The total input tokens must not exceed the limit.
    • Multi-turn dialogue: The last message's token count must not exceed the limit. If the total context (including history) exceeds the limit, the model will automatically 'forget' older historical information, retaining only the most recent context that fits within the token limit.