{"id":13593568,"url":"https://github.com/shroominic/funcchain","last_synced_at":"2025-04-04T17:07:51.752Z","repository":{"id":191872195,"uuid":"685523400","full_name":"shroominic/funcchain","owner":"shroominic","description":"⛓️ build cognitive systems, pythonic","archived":false,"fork":false,"pushed_at":"2024-06-30T20:14:43.000Z","size":5381,"stargazers_count":324,"open_issues_count":5,"forks_count":21,"subscribers_count":12,"default_branch":"main","last_synced_at":"2024-10-29T16:41:46.326Z","etag":null,"topics":["funcchain","jinja2","langchain","langsmith","llm","minimalistic","openai-functions","prompt","pydantic","python-async","pythonic"],"latest_commit_sha":null,"homepage":"https://shroominic.github.io/funcchain/","language":"Python","has_issues":true,"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/shroominic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":"roadmap.todo","authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-31T12:28:34.000Z","updated_at":"2024-10-29T04:05:51.000Z","dependencies_parsed_at":"2023-10-15T20:08:44.517Z","dependency_job_id":"1577fcbb-937a-4ccc-b86c-52a87804b023","html_url":"https://github.com/shroominic/funcchain","commit_stats":null,"previous_names":["shroominic/funcchain"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shroominic%2Ffuncchain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shroominic%2Ffuncchain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shroominic%2Ffuncchain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shroominic%2Ffuncchain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shroominic","download_url":"https://codeload.github.com/shroominic/funcchain/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247217183,"owners_count":20903009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["funcchain","jinja2","langchain","langsmith","llm","minimalistic","openai-functions","prompt","pydantic","python-async","pythonic"],"created_at":"2024-08-01T16:01:21.666Z","updated_at":"2025-04-04T17:07:51.730Z","avatar_url":"https://github.com/shroominic.png","language":"Python","readme":"\n# funcchain\n\n\u003c!-- markdownlint-disable MD033 --\u003e\n[![Version](https://badge.fury.io/py/funcchain.svg)](https://badge.fury.io/py/funcchain)\n[![tests](https://github.com/shroominic/funcchain/actions/workflows/code-check.yml/badge.svg)](https://github.com/shroominic/funcchain/actions/workflows/code-check.yml)\n![PyVersion](https://img.shields.io/pypi/pyversions/funcchain)\n![License](https://img.shields.io/github/license/shroominic/funcchain)\n![Downloads](https://img.shields.io/pypi/dm/funcchain)\n[![Discord](https://img.shields.io/discord/1192334452110659664?label=discord)](https://discord.gg/TrwWWMXdtR)\n\u003cimg alt=\"GitHub Contributors\" src=\"https://img.shields.io/github/contributors/shroominic/funcchain\" /\u003e\n\u003cimg alt=\"GitHub Last Commit\" src=\"https://img.shields.io/github/last-commit/shroominic/funcchain\" /\u003e\n[![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://docs.pydantic.dev/latest/contributing/#badges)\n[![Twitter Follow](https://img.shields.io/twitter/follow/shroominic?style=social)](https://x.com/shroominic)\n\u003c!-- \u003cimg alt=\"Repo Size\" src=\"https://img.shields.io/github/repo-size/shroominic/funcchain\" /\u003e --\u003e\n\n```bash\npip install funcchain\n```\n\n## Introduction\n\n`funcchain` is the *most pythonic* way of writing cognitive systems. Leveraging pydantic models as output schemas combined with langchain in the backend allows for a seamless integration of llms into your apps.\nIt utilizes OpenAI Functions or LlamaCpp grammars (json-schema-mode) for efficient structured output.\nIn the backend it compiles the funcchain syntax into langchain runnables so you can easily invoke, stream or batch process your pipelines.\n\n[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/ricklamers/funcchain-demo)\n\n## Simple Demo\n\n```python\nfrom funcchain import chain\nfrom pydantic import BaseModel\n\n# define your output shape\nclass Recipe(BaseModel):\n    ingredients: list[str]\n    instructions: list[str]\n    duration: int\n\n# write prompts utilising all native python features\ndef generate_recipe(topic: str) -\u003e Recipe:\n    \"\"\"\n    Generate a recipe for a given topic.\n    \"\"\"\n    return chain() # \u003c- this is doing all the magic\n\n# generate llm response\nrecipe = generate_recipe(\"christmas dinner\")\n\n# recipe is automatically converted as pydantic model\nprint(recipe.ingredients)\n```\n\n## Complex Structured Output\n\n```python\nfrom pydantic import BaseModel, Field\nfrom funcchain import chain\n\n# define nested models\nclass Item(BaseModel):\n    name: str = Field(description=\"Name of the item\")\n    description: str = Field(description=\"Description of the item\")\n    keywords: list[str] = Field(description=\"Keywords for the item\")\n\nclass ShoppingList(BaseModel):\n    items: list[Item]\n    store: str = Field(description=\"The store to buy the items from\")\n\nclass TodoList(BaseModel):\n    todos: list[Item]\n    urgency: int = Field(description=\"The urgency of all tasks (1-10)\")\n\n# support for union types\ndef extract_list(user_input: str) -\u003e TodoList | ShoppingList:\n    \"\"\"\n    The user input is either a shopping List or a todo list.\n    \"\"\"\n    return chain()\n\n# the model will choose the output type automatically\nlst = extract_list(\n    input(\"Enter your list: \")\n)\n\n# custom handler based on type\nmatch lst:\n    case ShoppingList(items=items, store=store):\n        print(\"Here is your Shopping List: \")\n        for item in items:\n            print(f\"{item.name}: {item.description}\")\n        print(f\"You need to go to: {store}\")\n\n    case TodoList(todos=todos, urgency=urgency):\n        print(\"Here is your Todo List: \")\n        for item in todos:\n            print(f\"{item.name}: {item.description}\")\n        print(f\"Urgency: {urgency}\")\n```\n\n## Vision Models\n\n```python\nfrom funcchain import Image\nfrom pydantic import BaseModel, Field\nfrom funcchain import chain, settings\n\n# set global llm using model identifiers (see MODELS.md)\nsettings.llm = \"openai/gpt-4-vision-preview\"\n\n# everything defined is part of the prompt\nclass AnalysisResult(BaseModel):\n    \"\"\"The result of an image analysis.\"\"\"\n\n    theme: str = Field(description=\"The theme of the image\")\n    description: str = Field(description=\"A description of the image\")\n    objects: list[str] = Field(description=\"A list of objects found in the image\")\n\n# easy use of images as input with structured output\ndef analyse_image(image: Image) -\u003e AnalysisResult:\n    \"\"\"\n    Analyse the image and extract its\n    theme, description and objects.\n    \"\"\"\n    return chain()\n\nresult = analyse_image(Image.open(\"examples/assets/old_chinese_temple.jpg\"))\n\nprint(\"Theme:\", result.theme)\nprint(\"Description:\", result.description)\nfor obj in result.objects:\n    print(\"Found this object:\", obj)\n```\n\n## Seamless local model support\n\n```python\nfrom pydantic import BaseModel, Field\nfrom funcchain import chain, settings\n\n# auto-download the model from huggingface\nsettings.llm = \"ollama/openchat\"\n\nclass SentimentAnalysis(BaseModel):\n    analysis: str\n    sentiment: bool = Field(description=\"True for Happy, False for Sad\")\n\ndef analyze(text: str) -\u003e SentimentAnalysis:\n    \"\"\"\n    Determines the sentiment of the text.\n    \"\"\"\n    return chain()\n\n# generates using the local model\npoem = analyze(\"I really like when my dog does a trick!\")\n\n# promised structured output (for local models!)\nprint(poem.analysis)\n```\n\n## Features\n\n- 🐍 pythonic\n- 🔀 easy swap between openai or local models\n- 🔄 dynamic output types (pydantic models, or primitives)\n- 👁️ vision llm support\n- 🧠 langchain_core as backend\n- 📝 jinja templating for prompts\n- 🏗️ reliable structured output\n- 🔁 auto retry parsing\n- 🔧 langsmith support\n- 🔄 sync, async, streaming, parallel, fallbacks\n- 📦 gguf download from huggingface\n- ✅ type hints for all functions and mypy support\n- 🗣️ chat router component\n- 🧩 composable with langchain LCEL\n- 🛠️ easy error handling\n- 🚦 enums and literal support\n- 📐 custom parsing types\n\n## Documentation\n\n[Checkout the docs here](https://shroominic.github.io/funcchain/) 👈\n\nAlso highly recommend to try and run the examples in the `./examples` folder.\n\n## Contribution\n\nYou want to contribute? Thanks, that's great!\nFor more information checkout the [Contributing Guide](docs/contributing/dev-setup.md).\nPlease run the dev setup to get started:\n\n```bash\ngit clone https://github.com/shroominic/funcchain.git \u0026\u0026 cd funcchain\n\n./dev_setup.sh\n```\n","funding_links":[],"categories":["Python","开源项目","Open Source Projects"],"sub_categories":["其他聊天机器人","Other / Chatbots"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshroominic%2Ffuncchain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshroominic%2Ffuncchain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshroominic%2Ffuncchain/lists"}