### Project Setup and Example Execution Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Provides command-line instructions for setting up the SimpleLLMFunc project. This includes installing dependencies, configuring API keys by copying and editing the `.env` file, and running various example scripts. ```bash # Install dependencies pip install SimpleLLMFunc # Set up API keys cp env_template .env # Edit .env file, enter your API keys # Run examples python examples/llm_function_example.py python examples/llm_chat_example.py python examples/parallel_toolcall_example.py ``` -------------------------------- ### Environment Variable Configuration Example Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md This bash snippet shows an example of an `.env` file used for configuring SimpleLLMFunc. It includes settings for log directory, log level, and Langfuse API keys, demonstrating how to manage application settings through environment variables. ```bash # .env file example LOG_DIR=./logs # Log directory (optional) LOG_LEVEL=INFO # Log level, only controls console log output, doesn't affect file log output LANGFUSE_PUBLIC_KEY=pk_xxx # Langfuse public key (optional) LANGFUSE_SECRET_KEY=sk_xxx # Langfuse secret key (optional) ``` -------------------------------- ### Example Usage of LLM Function Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Provides an example of how to call the `analyze_product_review` LLM function and process its output. It includes sample product data and error handling, demonstrating the direct return of a Pydantic object. ```python import asyncio from SimpleLLMFunc import llm_function # Assuming llm_function is imported here for context # Assuming ProductReview and analyze_product_review are defined as above # Assuming OpenAICompatible is defined and provider.json exists async def main(): app_log("Starting example code") # Assuming app_log is a defined logging function # Test product review analysis product_name = "XYZ Wireless Headphones" review_text = """ I've been using these XYZ wireless headphones for a month. The sound quality is very good, especially the bass performance is excellent, and they're comfortable to wear, can be used for long periods without fatigue. The battery life is also strong, can last about 8 hours after full charge. However, the connection is occasionally unstable, sometimes suddenly disconnects. Also, the touch controls are not sensitive enough, often need to click multiple times to respond. Overall, these headphones have great value for money, suitable for daily use, but if you need them for professional audio work, they might not be enough. """ try: print("\n===== Product Review Analysis =====") result = await analyze_product_review(product_name, review_text) # result is directly a Pydantic model instance # no need to deserialize print(f"Rating: {result.rating}/5") print("Advantages:") for pro in result.pros: print(f"- {pro}") print("Disadvantages:") for con in result.cons: print(f"- {con}") print(f"Summary: {result.summary}") except Exception as e: print(f"Product review analysis failed: {e}") if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Multimodal Tool Example with @tool (Python) Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Demonstrates defining multimodal tools using the `@tool` decorator, capable of returning different data types like image paths or URLs. This example includes tools for generating charts and searching web images. ```python from SimpleLLMFunc.tool import tool from SimpleLLMFunc.type import ImgPath, ImgUrl @tool(name="generate_chart", description="Generate charts based on data") async def generate_chart(data: str, chart_type: str = "bar") -> ImgPath: """ Generate charts based on provided data Args: data: CSV format data chart_type: Chart type, default is bar chart Returns: Generated chart file path """ # Actual implementation would generate chart and save locally chart_path = "./generated_chart.png" # ... Chart generation logic return ImgPath(chart_path) @tool(name="search_web_image", description="Search web images") async def search_web_image(query: str) -> ImgUrl: """ Search web images Args: query: Search keywords Returns: Found image URL """ # Actual implementation would call image search API image_url = "https://example.com/search_result.jpg" return ImgUrl(image_url) ``` -------------------------------- ### Provider Configuration JSON Example Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md A sample JSON file (`provider.json`) demonstrating how to configure different LLM providers like Deepseek and OpenAI. It includes details such as model names, API keys, base URLs, and retry/rate limiting parameters for each provider. ```json { "deepseek": [ { "model_name": "deepseek-v3.2", "api_keys": ["sk-your-api-key-1", "sk-your-api-key-2"], "base_url": "https://api.deepseek.com/v1", "max_retries": 5, "retry_delay": 1.0, "rate_limit_capacity": 10, "rate_limit_refill_rate": 1.0 } ], "openai": [ { "model_name": "gpt-4", "api_keys": ["sk-your-api-key"], "base_url": "https://api.openai.com/v1", "max_retries": 5, "retry_delay": 1.0, "rate_limit_capacity": 10, "rate_limit_refill_rate": 1.0 } ] } ``` -------------------------------- ### Logging Usage Example in Python Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Demonstrates basic usage of the logging utilities provided by SimpleLLMFunc, including importing the application logger, error pushing function, and context management. This is part of the system's observability features. ```python from SimpleLLMFunc.logger import app_log, push_error, log_context ``` -------------------------------- ### Implementing a Custom LLM Interface in Python Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Provides an example of how to create a custom LLM interface by inheriting from `SimpleLLMFunc.interface.LLM_Interface`. This allows users to implement their own logic for calling LLMs, offering maximum flexibility. ```python from SimpleLLMFunc.interface import LLM_Interface class CustomLLMInterface(LLM_Interface): async def call_llm(self, messages, **kwargs): # Implement your own LLM calling logic pass ``` -------------------------------- ### Project Structure of SimpleLLMFunc Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md This is a directory listing representing the project structure of SimpleLLMFunc. It outlines the organization of core modules, examples, and configuration files, facilitating understanding of the project's layout and maintainability. ```tree SimpleLLMFunc/ ├── SimpleLLMFunc/ │ ├── llm_decorator/ # LLM decorator module │ │ ├── llm_function_decorator.py # @llm_function implementation │ │ ├── llm_chat_decorator.py # @llm_chat implementation │ │ └── utils.py # Decorator utilities │ ├── tool/ # Tool system │ │ └── tool.py # @tool decorator and Tool base class │ ├── interface/ # LLM interface layer │ │ ├── llm_interface.py # Abstract base class │ │ ├── openai_compatible.py # OpenAI compatible implementation │ │ ├── key_pool.py # API key management │ │ └── token_bucket.py # Traffic control │ ├── base/ # Core execution engine │ │ ├── ReAct.py # ReAct engine and tool calls │ │ ├── messages.py # Message building │ │ ├── post_process.py # Response parsing and type conversion │ │ └── type_resolve.py # Type resolution │ ├── logger/ # Logging and observability │ │ ├── logger.py # Logging API │ │ ├── logger_config.py # Logging configuration │ │ └── context_manager.py # Context management │ ├── observability/ # Observability integration │ │ └── langfuse_client.py # Langfuse integration │ ├── type/ # Multimodal types │ │ └── __init__.py # Text, ImgUrl, ImgPath, etc. │ ├── config.py # Global configuration │ └── __init__.py # Package initialization and API exports ├── examples/ # Usage examples │ ├── llm_function_example.py # Basic examples │ ├── llm_chat_example.py # Chat examples │ ├── parallel_toolcall_example.py # Concurrency examples │ ├── multi_modality_toolcall.py # Multimodal examples │ ├── provider.json # Provider configuration examples │ └── provider_template.json # Configuration template ├── pyproject.toml # Poetry configuration ├── README.md # Project documentation (you are here) ├── CHANGELOG.md # Changelog └── env_template # Environment variable template ``` -------------------------------- ### Create an Intelligent Agent using @llm_chat Decorator (Python) Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md This Python code snippet illustrates the use of the `@llm_chat` decorator to create an intelligent agent. The agent can access tools like search and calculator, process user messages, and maintain conversation history. An example usage is provided. ```python @llm_chat(llm_interface=llm, toolkit=[search_tool, calculator_tool]) async def agent(user_message: str, history: List[Dict]) -> str: """Intelligent assistant that can search information and do math calculations""" pass # Usage response = await agent("What's the weather like in Beijing tomorrow? And calculate what temperature it would be if it drops 5 degrees", []) ``` -------------------------------- ### Extract Named Entities using @llm_function Decorator (Python) Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md This Python code snippet demonstrates how to use the `@llm_function` decorator to define a function that extracts named entities from text. It shows the function signature, docstring for LLM interpretation, and an example of its usage and expected output. ```python @llm_function(llm_interface=llm) async def extract_entities(text: str) -> Dict[str, List[str]]: """Extract named entities (people, places, organizations, etc.) from text""" pass # Usage entities = await extract_entities("John works at Apple in Beijing") # Returns: {"person": ["John"], "location": ["Beijing"], "organization": ["Apple"]} ``` -------------------------------- ### Basic Logging with SimpleLLMFunc Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Demonstrates basic logging functions like app_log and push_error. These functions can be used to log messages and errors, optionally with trace IDs and exception information. ```python app_log("Starting request processing", trace_id="request_123") push_error("Error occurred", trace_id="request_123", exc_info=True) ``` -------------------------------- ### Copy Configuration Template Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Copies the environment configuration template to a new file named .env. This is the first step in setting up the project's environment variables. ```bash cp env_template .env ``` -------------------------------- ### Integrating LLM Providers with OpenAICompatible in Python Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Shows how to integrate various LLM providers using the `OpenAICompatible` class in SimpleLLMFunc. It covers loading configurations from a JSON file and direct instantiation with API keys and base URLs. This enables flexible connection to different LLM services. ```python from SimpleLLMFunc import OpenAICompatible # Method 1: Load from JSON configuration file provider_config = OpenAICompatible.load_from_json_file("provider.json") llm = provider_config["deepseek"]["v3-turbo"] # Method 2: Direct creation llm = OpenAICompatible( api_key="sk-xxx", base_url="https://api.deepseek.com/v1", model="deepseek-chat" ) @llm_function(llm_interface=llm) async def my_function(text: str) -> str: """Process text""" pass ``` -------------------------------- ### Async LLM Function and Chat with Streaming Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Illustrates the native asynchronous design of SimpleLLMFunc, showcasing an async text analysis function and an async chat function with streaming capabilities. It requires importing `llm_function` and `llm_chat`. ```python from SimpleLLMFunc import llm_function, llm_chat from typing import List, Dict # Assuming my_llm_interface is a pre-configured LLM interface object @llm_function(llm_interface=my_llm_interface) async def async_analyze_text(text: str) -> str: """Async text content analysis""" pass @llm_chat(llm_interface=my_llm_interface, stream=True) async def async_chat(message: str, history: List[Dict[str, str]]): """Async chat functionality, supports streaming responses""" pass async def main(): result = await async_analyze_text("Text to analyze") async for response, updated_history in async_chat("Hello", []): print(response) ``` -------------------------------- ### Analyze Image with Multimodal Inputs using Python Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Demonstrates how to use the `llm_function` decorator to create a function that analyzes images from web URLs and local paths, incorporating text descriptions. It requires the `SimpleLLMFunc` library and its type hints. ```python from SimpleLLMFunc import llm_function from SimpleLLMFunc.type import ImgPath, ImgUrl, Text @llm_function(llm_interface=my_llm_interface) async def analyze_image( description: Text, # Text description web_image: ImgUrl, # Web image URL local_image: ImgPath # Local image path ) -> str: """Analyze images and provide detailed explanations based on descriptions Args: description: Specific requirements for image analysis web_image: Web image URL to analyze local_image: Local reference image path for comparison Returns: Detailed image analysis results """ pass import asyncio async def main(): result = await analyze_image( description=Text("Please describe the differences between these two images in detail"), web_image=ImgUrl("https://example.com/image.jpg"), local_image=ImgPath("./reference.jpg") ) print(result) asyncio.run(main()) ``` -------------------------------- ### Tool Definition with Class Inheritance (Python) Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Illustrates an alternative method for defining tools by inheriting from the `Tool` base class. This approach is suitable for more complex logic or specific requirements. ```python from SimpleLLMFunc.tool import Tool class WebSearchTool(Tool): def __init__(self): super().__init__( name="web_search", description="Search information on the internet" ) async def run(self, query: str, max_results: int = 5) -> dict: """Execute web search""" # Implement search logic return {"results": [...]} ``` -------------------------------- ### Context Manager Logging in SimpleLLMFunc Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Illustrates using a context manager (log_context) to automatically associate logs with a specific trace ID and function name. This simplifies logging within blocks of code where context is consistent. ```python with log_context(trace_id="task_456", function_name="analyze_text"): app_log("Starting text analysis") # Automatically inherits context trace_id try: # Execute operations... app_log("Analysis completed") except Exception: push_error("Analysis failed", exc_info=True) # Also automatically inherits trace_id ``` -------------------------------- ### Integrating Tools into LLM Functions (Python) Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Shows how to integrate defined tools into LLM functions like `answer_question` using decorators such as `@llm_function`. This allows LLMs to utilize the defined tools to answer questions or perform tasks. ```python @llm_function( llm_interface=llm, toolkit=[get_weather, search_web, WebSearchTool()], ) async def answer_question(question: str) -> str: """ Answer user questions, use tools when necessary. Args: question: User's question Returns: Answer """ pass ``` -------------------------------- ### Multimodal Content Analysis in Python Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Shows how to define an LLM function for analyzing images, accepting both local file paths (`ImgPath`) and web URLs (`ImgUrl`) as input. This enables comparison and analysis of visual content. ```python from SimpleLLMFunc.type import ImgPath, ImgUrl @llm_function(llm_interface=llm) async def analyze_images(local_img: ImgPath, web_img: ImgUrl) -> str: """Compare and analyze two images""" pass ``` -------------------------------- ### Tool Definition with @tool Decorator (Python) Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Shows how to define LLM-accessible tools using the `@tool` decorator. This method is recommended for its conciseness and automatic extraction of parameter information. It supports Pydantic models for complex parameters and async functions. ```python from pydantic import BaseModel, Field from SimpleLLMFunc.tool import tool class Location(BaseModel): latitude: float = Field(..., description="Latitude") longitude: float = Field(..., description="Longitude") @tool(name="get_weather", description="Get weather information for specified location") async def get_weather(location: Location, days: int = 1) -> dict: """ Get weather forecast for specified location Args: location: Location information, including latitude and longitude days: Forecast days, default is 1 day Returns: Weather forecast information """ # Actual implementation would call weather API return { "location": f"{location.latitude},{location.longitude}", "forecast": [{"day": i, "temp": 25, "condition": "Sunny"} for i in range(days)] } ``` -------------------------------- ### Batch Data Processing with Asyncio in Python Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Demonstrates how to process a batch of texts asynchronously using the `llm_function` decorator and `asyncio.gather`. This method is efficient for parallel execution of LLM tasks on multiple inputs. ```python import asyncio @llm_function(llm_interface=llm) async def classify_text(text: str) -> str: """Classify text""" pass # Batch processing, fully utilize async texts = ["Text 1", "Text 2", "Text 3", ...] results = await asyncio.gather(*[classify_text(t) for t in texts]) ``` -------------------------------- ### Advanced Decorator Configuration for LLM Functions in Python Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Illustrates advanced configuration options for the `@llm_function` decorator, including specifying LLM interfaces, toolkits, dynamic prompt template parameters, automatic retries, and timeouts. This allows for more sophisticated control over LLM function behavior. ```python @llm_function( llm_interface=llm_interface, # LLM interface instance toolkit=[tool1, tool2], # Tool list _template_params={ "language": "English", "style": "Professional" }, retry_on_exception=True, # Auto retry on exception timeout=60 # Timeout setting ) async def my_function(param: str) -> str: """Supports {language} {style} analysis""" pass ``` -------------------------------- ### Create LLM Function with Decorator Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Demonstrates how to define an LLM function using the `@llm_function` decorator. This function analyzes product reviews and returns a structured report. It specifies the LLM interface and uses type hints for input and output. ```python from SimpleLLMFunc import llm_function from pydantic import BaseModel class ProductReview(BaseModel): rating: int pros: list[str] cons: list[str] summary: str @llm_function( llm_interface=OpenAICompatible.load_from_json_file("provider.json")["volc_engine"]["deepseek-v3-250324"] ) async def analyze_product_review(product_name: str, review_text: str) -> ProductReview: """You are a professional product review expert who needs to objectively analyze the following product review and generate a structured review report. The report should include: 1. Overall product rating (1-5 points) 2. List of main product advantages 3. List of main product disadvantages 4. Summary evaluation Rating rules: - 5 points: Perfect, almost no disadvantages - 4 points: Excellent, advantages clearly outweigh disadvantages - 3 points: Average, advantages and disadvantages are basically equal - 2 points: Poor, disadvantages clearly outweigh advantages - 1 point: Very poor, almost no advantages Args: product_name: Name of the product to review review_text: User's review content of the product Returns: A structured ProductReview object containing rating, advantages list, disadvantages list, and summary """ pass # Prompt as Code, Code as Doc ``` -------------------------------- ### Configure Rate Limiting Parameters in provider.json Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md This JSON snippet demonstrates how to configure rate limiting parameters for an LLM model. It specifies the maximum requests per minute and the maximum burst requests allowed. These settings are crucial for managing API traffic and preventing rate limiting. ```json { "model_config": { "rate_limit": 100, // Maximum 100 requests per minute "burst": 10 // Maximum 10 burst requests } } ``` -------------------------------- ### Python LLM Function with Pydantic Model for Product Review Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Illustrates the use of the @llm_function decorator with a Pydantic model for structured output. The function is designed to parse product reviews into a defined schema, including rating, pros, cons, and a summary. This showcases type safety and structured data handling in LLM functions. ```python """ Example using LLM function decorator """ import asyncio from typing import List from pydantic import BaseModel, Field from SimpleLLMFunc import llm_function, OpenAICompatible, app_log # Define a Pydantic model as return type class ProductReview(BaseModel): rating: int = Field(..., description="Product rating, 1-5 points") pros: List[str] = Field(..., description="List of product advantages") cons: List[str] = Field(..., description="List of product disadvantages") summary: str = Field(..., description="Review summary") ``` -------------------------------- ### BibTeX Citation for SimpleLLMFunc Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md A BibTeX entry for citing the SimpleLLMFunc project in academic or research work. It includes author, title, URL, version, and publication year. ```bibtex @software{ni2025simplellmfunc, author = {Jingzhe Ni}, month = {October}, title = {{SimpleLLMFunc: A New Approach to Build LLM Applications}}, url = {https://github.com/NiJingzhe/SimpleLLMFunc}, version = {0.5.0.beta1}, year = {2025} } ``` -------------------------------- ### Python LLM Function for Sentiment Analysis Source: https://github.com/nijingzhe/simplellmfunc/blob/master/README.md Demonstrates how to use the @llm_function decorator to create a sentiment analysis function. It loads an LLM interface from a JSON file and defines an asynchronous function that takes text and returns a sentiment classification. Requires the SimpleLLMFunc library and an OpenAI-compatible LLM provider configuration. ```python import asyncio from SimpleLLMFunc import llm_function, OpenAICompatible # Load LLM interface from configuration file llm = OpenAICompatible.load_from_json_file("provider.json")["your_provider"]["model"] @llm_function(llm_interface=llm) async def classify_sentiment(text: str) -> str: """ Analyze the sentiment tendency of text. Args: text: Text to analyze Returns: Sentiment classification, can be 'positive', 'negative', or 'neutral' """ pass # Prompt as Code! async def main(): result = await classify_sentiment("This product is amazing!") print(f"Sentiment classification: {result}") asyncio.run(main()) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.