{"id":26261893,"url":"https://github.com/deverman/langchainswiftclidemo","last_synced_at":"2026-04-27T09:31:44.641Z","repository":{"id":281787819,"uuid":"946396801","full_name":"deverman/langchainswiftclidemo","owner":"deverman","description":"Test of using Langchain Swift Library","archived":false,"fork":false,"pushed_at":"2025-03-11T10:20:19.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-30T17:36:09.617Z","etag":null,"topics":["cli","command-line-tool","langchain","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/deverman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-11T04:30:41.000Z","updated_at":"2025-03-11T10:20:22.000Z","dependencies_parsed_at":"2025-03-11T06:41:59.877Z","dependency_job_id":null,"html_url":"https://github.com/deverman/langchainswiftclidemo","commit_stats":null,"previous_names":["deverman/langchainswiftclidemo"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/deverman/langchainswiftclidemo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deverman%2Flangchainswiftclidemo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deverman%2Flangchainswiftclidemo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deverman%2Flangchainswiftclidemo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deverman%2Flangchainswiftclidemo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deverman","download_url":"https://codeload.github.com/deverman/langchainswiftclidemo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deverman%2Flangchainswiftclidemo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32331305,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T23:26:28.701Z","status":"online","status_checked_at":"2026-04-27T02:00:06.769Z","response_time":128,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","command-line-tool","langchain","swift"],"created_at":"2025-03-14T00:16:31.346Z","updated_at":"2026-04-27T09:31:44.589Z","avatar_url":"https://github.com/deverman.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LangChain \u0026 LangGraph Swift Tutorial\n\nA comprehensive tutorial demonstrating how to build an AI assistant using LangChain Swift and LangGraph. This example shows how to create a workflow-based AI application that combines custom tools with OpenAI's API.\n\n## What You'll Learn\n\n- How to use LangChain Swift for building AI applications\n- How to create and manage workflows with LangGraph\n- How to create custom tools and integrate them with LangChain\n- How to build a command-line interface for AI applications\n- How to handle state management in AI workflows\n- Best practices for error handling and resource management\n\n## Prerequisites\n\n- macOS 13.0 or later\n- Swift 5.9 or later\n- OpenAI API key (get one at https://platform.openai.com)\n\n## Project Structure\n\n```\n.\n├── Sources/\n│   └── main.swift      # Main implementation with LangChain and LangGraph integration\n├── Package.swift       # Dependencies\n└── README.md          # This file\n```\n\n## Architecture Overview\n\nThe project demonstrates a modern agentic AI architecture with these key components:\n\n1. **State Management** (`AssistantState`)\n   - Manages workflow state using LangGraph channels\n   - Tracks messages, tool executions, and completion status\n   - Provides type-safe access to state data\n\n2. **Tool System**\n   - Defines a `Tool` protocol for creating custom tools\n   - Each tool has a name, description, and execution logic\n   - Tools are automatically discovered by the LLM\n\n3. **Query Processing**\n   - Uses LangChain for LLM integration\n   - Implements tool selection and execution\n   - Handles resource management and cleanup\n\n4. **Command Line Interface**\n   - Built with ArgumentParser\n   - Supports query execution and verbose mode\n   - Provides clear error messages\n\n## Adding Your Own Tools\n\nTo add a new tool to the system:\n\n1. Create a new class implementing the `Tool` protocol:\n```swift\nclass MyNewTool: Tool {\n    func name() -\u003e String {\n        return \"my_tool_name\" // The name the LLM will use to identify this tool\n    }\n    \n    func description() -\u003e String {\n        return \"A clear description of what this tool does and how to use it\"\n    }\n    \n    func run(_ input: String) async throws -\u003e String {\n        // Your tool's logic here\n        // Process the input and return a result\n        return \"Result\"\n    }\n}\n```\n\n2. Add your tool to the tools array in `AIAssistant.run()`:\n```swift\nlet tools: [any Tool] = [\n    TimeCheckTool(),\n    CalculatorTool(),\n    MyNewTool()  // Add your new tool here\n]\n```\n\nThe LLM will automatically discover your tool and use it when appropriate based on its name and description.\n\n### Tool Design Best Practices\n\n1. **Clear Names**: Use descriptive names that clearly indicate the tool's purpose\n2. **Detailed Descriptions**: Provide clear descriptions that help the LLM understand when to use the tool\n3. **Input Validation**: Implement robust input validation in the `run` method\n4. **Error Handling**: Use clear error messages that help diagnose issues\n5. **Resource Management**: Clean up any resources your tool uses\n\n## Running the Application\n\n1. Set up your environment:\n```bash\nexport OPENAI_API_KEY='your-api-key-here'\n```\n\n2. Build the project:\n```bash\nswift build\n```\n\n3. Try different examples:\n```bash\n# Get the current time\n.build/debug/langchainswiftclidemo --query \"What time is it?\"\n\n# Do a calculation\n.build/debug/langchainswiftclidemo --query \"Calculate 5 * 3\"\n\n# Try both tools at once\n.build/debug/langchainswiftclidemo --query \"What time is it and calculate 15 * 24?\"\n\n# See available tools\n.build/debug/langchainswiftclidemo --verbose\n```\n\n## Error Handling\n\nThe system implements several layers of error handling:\n\n1. **Tool-level errors**: Each tool implements its own error handling\n2. **Query processing errors**: The `QueryProcessor` handles LLM and execution errors\n3. **Resource management**: Automatic cleanup of HTTP clients and event loops\n4. **Input validation**: Command-line argument validation\n\n## Resource Management\n\nThe system automatically manages:\n- HTTP client lifecycle\n- Event loop groups\n- Tool resources\n- Memory cleanup\n\n## Contributing\n\nWe welcome contributions! Please see our contributing guidelines for details.\n\n## License\n\nThis project is available under the MIT license. ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeverman%2Flangchainswiftclidemo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeverman%2Flangchainswiftclidemo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeverman%2Flangchainswiftclidemo/lists"}