### Install and Run fakellm Source: https://github.com/1dg618/fakellm/blob/main/README.md Install fakellm using pip and then initialize it to create a configuration file. Finally, start the mock server. ```bash pip install fakellm fakellm init # creates fakellm.yaml fakellm serve # starts on http://127.0.0.1:9999 ``` -------------------------------- ### Install Fakellm Source: https://github.com/1dg618/fakellm/blob/main/README.md Install the Fakellm package using pip. Requires Python 3.10 or higher. ```bash pip install fakellm ``` -------------------------------- ### Initialize and Serve Fakellm Source: https://github.com/1dg618/fakellm/blob/main/README.md Initialize Fakellm by creating a configuration file and start the server. Configuration changes can be reloaded without restarting the server. ```bash fakellm init # creates fakellm.yaml in the current directory fakellm serve # starts the server on 127.0.0.1:9999 ``` -------------------------------- ### Fakellm Configuration Structure Source: https://github.com/1dg618/fakellm/blob/main/README.md Example of the top-level structure for the fakellm.yaml configuration file, including version, defaults, and rules. ```yaml version: 1 defaults: fallback: deterministic_echo # what to return when no rule matches rules: - name: my_rule when: { ... } # conditions (all must match) respond: { ... } # what to return ``` -------------------------------- ### Install fakellm with accurate token counting Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Install fakellm with the optional 'accurate' dependency for tiktoken-based token counts. This enables precise token counting for OpenAI models and approximate counts for Anthropic models when tiktoken is available. ```bash pip install fakellm[accurate] ``` -------------------------------- ### fakellm CLI serving command Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Serve the fakellm application using the CLI. This command starts the fakellm server, which can be configured with options like the number of workers. ```bash fakellm serve ``` -------------------------------- ### fakellm YAML rule example with conversation state matching Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Example of a fakellm rule defined in YAML that matches on conversation state. This includes matchers for turn number, previous message role, and tool result content. ```yaml rules: - when: turn: 1 then: content: "Hello! How can I help you today?" - when: turn_in: [2, 3] previous_message_role: tool tool_result_contains: "search results" then: content: "I found some information for you." - when: previous_message_contains: "summarize this" then: content: "Here is a summary." ``` -------------------------------- ### fakellm rule with tool call mocking Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Example of a fakellm rule demonstrating tool call mocking. This includes mocking tool arguments and supporting chunked argument streaming. ```yaml rules: - when: messages_contain: - role: user content: "Find a restaurant near me." then: tool_calls: - id: "call_123" type: "function" function: name: "search_restaurants" arguments: "{\"location\": \"nearby\"}" ``` -------------------------------- ### fakellm rule with streaming support (OpenAI SSE format) Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Example of a fakellm rule demonstrating streaming support using the OpenAI Server-Sent Events (SSE) format. This simulates streaming responses for chat completions. ```yaml rules: - when: messages_contain: - role: user content: "Stream response." then: streaming: - data: "{\"choices\": [{\"delta\": {\"role\": \"assistant\"}, \"finish_reason\": null, \"index\": 0}]}" - data: "{\"choices\": [{\"delta\": {\"content\": \"This is a \"}, \"finish_reason\": null, \"index\": 0}]}" - data: "{\"choices\": [{\"delta\": {\"content\": \"streaming \"}, \"finish_reason\": null, \"index\": 0}]}" - data: "{\"choices\": [{\"delta\": {\"content\": \"response.\"}, \"finish_reason\": \"stop\"}]}" ``` -------------------------------- ### Get fakellm active conversations Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Retrieve a JSON snapshot of all active conversations in fakellm. This includes the turn count and tool-result count for each conversation. ```bash GET /_fakellm/conversations ``` -------------------------------- ### fakellm rule with streaming support (Anthropic typed event format) Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Example of a fakellm rule demonstrating streaming support using the Anthropic typed event sequence format. This simulates streaming responses for Anthropic messages. ```yaml rules: - when: messages_contain: - role: user content: "Stream Anthropic response." then: streaming: - event: "message_start" data: "{\"type\": \"message_start\", \"message\": {\"id\": \"msg_abc\", \"type\": \"message\", \"role\": \"assistant\", \"content\": [], \"model\": \"claude-3-opus-20240229\", \"stop_reason\": null, \"stop_sequence\": null, \"usage\": {\"input_tokens\": 10, \"output_tokens\": 1}}}" - event: "content_block_start" data: "{\"type\": \"content_block_start\", \"content_block\": {\"type\": \"text\", \"text\": \""}}" - event: "content_block_delta" data: "{\"type\": \"content_block_delta\", \"delta\": {\"type\": \"text_delta\", \"text\": \"This is an \"}}" - event: "content_block_delta" data: "{\"type\": \"content_block_delta\", \"delta\": {\"type\": \"text_delta\", \"text\": \"Anthropic \"}}" - event: "content_block_delta" data: "{\"type\": \"content_block_delta\", \"delta\": {\"type\": \"text_delta\", \"text\": \"streaming \"}}" - event: "content_block_delta" data: "{\"type\": \"content_block_delta\", \"delta\": {\"type\": \"text_delta\", \"text\": \"response.\"}}" - event: "content_block_stop" data: "{\"type\": \"content_block_stop\"}" - event: "message_delta" data: "{\"type\": \"message_delta\", \"delta\": {\"stop_reason\": \"end_turn\", \"usage\": {\"output_tokens\": 10}}}" - event: "message_stop" data: "{\"type\": \"message_stop\", \"message\": {\"id\": \"msg_abc\", \"type\": \"message\", \"role\": \"assistant\", \"content\": [ { \"type\": \"text\", \"text\": \"This is an Anthropic streaming response.\" } ], \"model\": \"claude-3-opus-20240229\", \"stop_reason\": \"end_turn\", \"stop_sequence\": null, \"usage\": {\"input_tokens\": 10, \"output_tokens\": 10 }}}" ``` -------------------------------- ### fakellm rule matching on individual request shape Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Example of fakellm rules that match on individual request shapes using `messages_contain`, `model_matches`, `tools_include`, and `header.*` matchers. These rules are compatible with existing configurations. ```yaml rules: - when: messages_contain: - role: user content: "What is the weather today?" then: content: "The weather is sunny." - when: model_matches: "gpt-4o" then: content: "Using GPT-4o." - when: tools_include: "search" then: content: "Performing a search." - when: header.X-Custom-Header: "test-value" then: content: "Custom header matched." ``` -------------------------------- ### fakellm rule with per-rule status codes Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Example of a fakellm rule that specifies per-rule status codes for testing error responses. This allows simulating different HTTP status codes for specific rule matches. ```yaml rules: - when: messages_contain: - role: user content: "Trigger error." then: status_code: 400 content: "Bad request." ``` -------------------------------- ### fakellm CLI initialization Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Initialize a new fakellm project using the CLI. This command sets up the basic project structure. ```bash fakellm init ``` -------------------------------- ### Fakellm CLI Commands Source: https://github.com/1dg618/fakellm/blob/main/README.md Common commands for initializing, serving, and configuring the Fakellm server. Use `--port`, `--host`, `--config`, `--reload`, or `--workers` for customization. ```bash fakellm init # create fakellm.yaml ``` ```bash fakellm serve # start the server ``` ```bash fakellm serve --port 8080 # custom port ``` ```bash fakellm serve --host 0.0.0.0 # custom host ``` ```bash fakellm serve --config x.yaml # custom config path ``` ```bash fakellm serve --reload # auto-reload on code changes (dev only) ``` ```bash fakellm serve --workers N # uvicorn workers (see caveat below; default 1) ``` -------------------------------- ### Configure OpenAI Client for fakellm Source: https://github.com/1dg618/fakellm/blob/main/README.md Configure the OpenAI client to point to the fakellm mock server instead of the real API. Ensure the base URL and API key are set correctly for your local fakellm instance. ```python from openai import OpenAI client = OpenAI(base_url="http://127.0.0.1:9999/v1", api_key="not-used") ``` -------------------------------- ### OpenAI Chat Completions Source: https://github.com/1dg618/fakellm/blob/main/README.md Provides mock OpenAI chat completions. Supports streaming responses. ```APIDOC ## POST /v1/chat/completions ### Description Mock endpoint for OpenAI chat completions. Supports streaming. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Query Parameters - **stream** (boolean) - Optional - If true, enables streaming responses. ### Request Example ```json { "model": "gpt-3.5-turbo", "messages": [ {"role": "user", "content": "Hello!"} ], "stream": true } ``` ### Response #### Success Response (200) - **choices** (array) - List of completion choices. - **usage** (object) - Token usage information. #### Response Example ```json { "id": "chatcmpl-123", "object": "chat.completion", "created": 1677652288, "model": "gpt-3.5-turbo", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Hello there! How can I help you today?" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30 } } ``` ``` -------------------------------- ### OpenAI API endpoint for chat completions Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md fakellm provides an endpoint compatible with the OpenAI API for chat completions. This allows testing of applications that use the OpenAI chat completion interface. ```http /v1/chat/completions ``` -------------------------------- ### Test Agent with fakellm Mock Server Source: https://github.com/1dg618/fakellm/blob/main/README.md This Python test uses pytest and httpx to interact with the fakellm mock server. It resets the server before the test and asserts that the agent's response contains expected content after a simulated research prompt. ```python import httpx import pytest from openai import OpenAI @pytest.fixture(autouse=True) def reset_fakellm(): httpx.post("http://127.0.0.1:9999/_fakellm/reset") def test_agent_handles_search(): client = OpenAI(base_url="http://127.0.0.1:9999/v1", api_key="not-used") result = run_my_agent(client, prompt="Please research fakellm") assert "found what you were looking for" in result ``` -------------------------------- ### Define Multi-turn Agent Flow with fakellm Rules Source: https://github.com/1dg618/fakellm/blob/main/README.md This YAML configuration defines two turns of an agent's conversation. The first turn triggers a tool call when the user message contains 'research'. The second turn responds with a summary after a tool result is received. ```yaml rules: # Turn 1: user asks for research → return a tool call - name: kickoff_research when: turn: 1 messages_contain: "research" respond: tool_calls: - name: web_search arguments: {query: "fakellm"} # Turn 2: tool result came back → return a summary - name: summarize_results when: turn: 2 tool_result_contains: "found" respond: content: "Based on the search, I found what you were looking for." ``` -------------------------------- ### Reload Configuration Source: https://github.com/1dg618/fakellm/blob/main/README.md Reloads the FakeLLM configuration from the YAML file. ```APIDOC ## POST /_fakellm/reload ### Description Triggers a reload of the `fakellm.yaml` configuration file, applying any changes without restarting the server. ### Method POST ### Endpoint /_fakellm/reload ``` -------------------------------- ### HTML Dashboard Source: https://github.com/1dg618/fakellm/blob/main/README.md Provides an HTML dashboard for viewing FakeLLM status and information. ```APIDOC ## GET /_fakellm ### Description Displays an HTML dashboard with an overview of the FakeLLM server status and configuration. ### Method GET ### Endpoint /_fakellm ``` -------------------------------- ### Reload fakellm configuration Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Trigger a hot reload of the fakellm configuration. This is useful for applying changes to rules or settings without restarting the server. ```bash POST /_fakellm/reload ``` -------------------------------- ### Reload Fakellm Configuration Source: https://github.com/1dg618/fakellm/blob/main/README.md Reload the Fakellm configuration after making changes to fakellm.yaml. This can be done by restarting the server or sending a POST request to the reload endpoint. ```bash curl -X POST http://127.0.0.1:9999/_fakellm/reload ``` -------------------------------- ### Anthropic API endpoint for messages Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md fakellm provides an endpoint compatible with the Anthropic API for messages. This allows testing of applications that use the Anthropic messages interface. ```http /v1/messages ``` -------------------------------- ### fakellm dashboard access Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Access the fakellm HTML dashboard. The dashboard displays request statistics and history, and can show active conversations and per-request turn numbers. ```bash /_fakellm ``` -------------------------------- ### Reset Conversations Source: https://github.com/1dg618/fakellm/blob/main/README.md Clears all conversation state stored by FakeLLM. ```APIDOC ## POST /_fakellm/reset ### Description Resets and clears all stored conversation history and state within FakeLLM. ### Method POST ### Endpoint /_fakellm/reset ``` -------------------------------- ### Statistics Endpoint Source: https://github.com/1dg618/fakellm/blob/main/README.md Retrieves statistics about requests, recent requests, and conversations in JSON format. ```APIDOC ## GET /_fakellm/stats ### Description Returns request counts, recent request details, and conversation statistics in JSON format. ### Method GET ### Endpoint /_fakellm/stats ### Response #### Success Response (200) - **request_counts** (object) - Counts of various request types. - **recent_requests** (array) - List of recent requests made to the server. - **conversations** (object) - Statistics related to ongoing conversations. ``` -------------------------------- ### Override Conversation ID in Python Source: https://github.com/1dg618/fakellm/blob/main/README.md Use the `X-Fakellm-Conversation-Id` header to manually set a conversation ID. This is useful for testing scenarios requiring explicit control over conversation tracking. ```python client.chat.completions.create( model="gpt-4", messages=[...], extra_headers={"X-Fakellm-Conversation-Id": "test-session-42"}, ) ``` -------------------------------- ### fakellm conversation ID header Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md The `X-Fakellm-Conversation-Id` header is sent on every response to indicate the conversation ID. Clients can also send this header on requests to override the auto-derived ID, which is useful for testing. ```http X-Fakellm-Conversation-Id ``` -------------------------------- ### Anthropic Messages Source: https://github.com/1dg618/fakellm/blob/main/README.md Provides mock Anthropic messages endpoint. Supports streaming responses. ```APIDOC ## POST /v1/messages ### Description Mock endpoint for Anthropic messages. Supports streaming. ### Method POST ### Endpoint /v1/messages ### Parameters #### Query Parameters - **stream** (boolean) - Optional - If true, enables streaming responses. ### Request Example ```json { "model": "claude-2.1", "messages": [ {"role": "user", "content": "Hello!"} ], "stream": true } ``` ### Response #### Success Response (200) - **content** (array) - List of content blocks. - **stop_reason** (string) - Reason for stopping generation. #### Response Example ```json { "id": "msg_01ABCDEFG12345", "type": "message", "role": "assistant", "model": "claude-2.1", "stop_reason": "end_turn", "stop_sequence": null, "usage": { "input_tokens": 10, "output_tokens": 25 }, "content": [ { "type": "text", "text": "Hello there! How can I assist you?" } ] } ``` ``` -------------------------------- ### Conversations Endpoint Source: https://github.com/1dg618/fakellm/blob/main/README.md Retrieves detailed information about conversations, including turn counts and tool results. ```APIDOC ## GET /_fakellm/conversations ### Description Provides a JSON response detailing the turn count and tool results for each conversation. ### Method GET ### Endpoint /_fakellm/conversations ### Response #### Success Response (200) - **conversations** (array) - List of conversations, each with turn count and tool results. ``` -------------------------------- ### Reset fakellm conversation state Source: https://github.com/1dg618/fakellm/blob/main/CHANGELOG.md Clear all conversation state in fakellm. This endpoint is useful for resetting the state between different test scenarios. ```bash POST /_fakellm/reset ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.