{"id":13577236,"url":"https://github.com/eyurtsev/kor","last_synced_at":"2025-05-13T19:10:26.482Z","repository":{"id":139650019,"uuid":"602715785","full_name":"eyurtsev/kor","owner":"eyurtsev","description":"LLM(😽) ","archived":false,"fork":false,"pushed_at":"2025-02-03T19:54:11.000Z","size":3145,"stargazers_count":1667,"open_issues_count":26,"forks_count":92,"subscribers_count":15,"default_branch":"main","last_synced_at":"2025-04-28T00:56:44.500Z","etag":null,"topics":["information-extraction","llm","natural-language","natural-language-processing","natural-language-understanding"],"latest_commit_sha":null,"homepage":"https://eyurtsev.github.io/kor/","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/eyurtsev.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":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-16T19:47:25.000Z","updated_at":"2025-04-27T16:27:39.000Z","dependencies_parsed_at":"2023-07-03T02:24:11.930Z","dependency_job_id":"57b33d16-d654-4acb-98b9-3969f65d117f","html_url":"https://github.com/eyurtsev/kor","commit_stats":{"total_commits":211,"total_committers":10,"mean_commits":21.1,"dds":0.05213270142180093,"last_synced_commit":"89a50f63e91345572a1cd066c65eff9005f083a1"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyurtsev%2Fkor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyurtsev%2Fkor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyurtsev%2Fkor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyurtsev%2Fkor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eyurtsev","download_url":"https://codeload.github.com/eyurtsev/kor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254010823,"owners_count":21998993,"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":["information-extraction","llm","natural-language","natural-language-processing","natural-language-understanding"],"created_at":"2024-08-01T15:01:19.604Z","updated_at":"2025-05-13T19:10:26.458Z","avatar_url":"https://github.com/eyurtsev.png","language":"Python","funding_links":[],"categories":["Python","Structured output"],"sub_categories":[],"readme":"[![Unit Tests](https://github.com/eyurtsev/kor/actions/workflows/test.yml/badge.svg?branch=main\u0026event=push)](https://github.com/eyurtsev/kor/actions/workflows/test.yml)\n[![Test Docs](https://github.com/eyurtsev/kor/actions/workflows/doc_test.yaml/badge.svg?branch=main\u0026event=push)](https://github.com/eyurtsev/kor/actions/workflows/doc_test.yaml)\n[![Release Notes](https://img.shields.io/github/release/eyurtsev/kor)](https://github.com/eyurtsev/kor/releases)\n[![Downloads](https://static.pepy.tech/badge/kor/month)](https://pepy.tech/project/kor)\n[![Open Issues](https://img.shields.io/github/issues-raw/eyurtsev/kor)](https://github.com/eyurtsev/kor/issues)\n[![](https://dcbadge.vercel.app/api/server/6adMQxSpJS?compact=true\u0026style=flat)](https://discord.com/channels/1038097195422978059/1170024642245832774)\n\n\u003e [!WARNING]\n\u003e If you're using a chat model that supports a tool calling API, you should probably be using the chat models' tool calling API instead of Kor!\n\u003e\n\u003e Kor is best suited for old style LLMs that did not have a chat interface and did not support tool calling.\n\u003e \n\u003e Read the [tool calling guide](https://python.langchain.com/docs/concepts/tool_calling/) in LangChain for more details.\n\u003e\n\u003e Please refer to the [chat model integration table](https://python.langchain.com/docs/integrations/chat/#featured-providers) for a list of chat models that support native tool calling.\n\n\n# Kor\n\nThis is a half-baked prototype that \"helps\" you extract structured data from text using LLMs 🧩.\n\nSpecify the schema of what should be extracted and provide some examples.\n\nKor will generate a prompt, send it to the specified LLM and parse out the\noutput.\n\nYou might even get results back.\n\nSo yes – it’s just another wrapper on top of LLMs with its own flavor of abstractions. 😸\n\nSee [documentation](https://eyurtsev.github.io/kor/).\n\nIntegrated with the [LangChain framework 😽💗 🦜🔗](https://python.langchain.com/en/latest/index.html).\n\n## Kor vs. LangChain\n\nThere are 3 different approaches for extracting information using LLMs: \n\n1. [prompt based/parsing](https://python.langchain.com/docs/use_cases/extraction/how_to/parse)\n2. [function/tool calling](https://python.langchain.com/docs/use_cases/extraction/quickstart)\n3. JSON mode\n\nPlease see the LangChain [extraction use case docs](https://python.langchain.com/docs/use_cases/extraction/) for an overview.\n\nKor has a pretty good implementation of the **parsing** approach. The approach works with all good-enough LLMs regardless\nof whether they support function/tool calling or JSON modes.\n\nExtraction quality is principally driven by providing good reference examples and good schema documentation.\n\nPlease see guidelines [here](https://python.langchain.com/docs/use_cases/extraction/guidelines) and [here](https://eyurtsev.github.io/kor/guidelines.html).\n\n## Version 1.0.0 Release\n\n* `kor` compatible with both pydantic v2 and v1.\n* pydantic v2 had significant breaking changes w/ respect to v1, `kor` major\n  version bump was used as a precaution.\n\n\nMain things to watch out for:\n\n1. Use a `default` value for any Optional fields if using pydantic v2 for validation.\n\n```python\nclass MusicRequest(BaseModel):\n    song: Optional[List[str]] = Field(\n        default=None,\n        description=\"The song(s) that the user would like to be played.\"\n    )\n```\n\n2. Kor schema is typed checked using pydantic. Pydantic v2 is stricter, and may\n   catch issues that were hiding in existing user code that was using the `kor`\n   library.\n\n3. Serialization has not yet been implemented with pydantic v2.\n\n\n## Kor style schema\n\n```python\nfrom langchain.chat_models import ChatOpenAI\nfrom kor import create_extraction_chain, Object, Text\n\nllm = ChatOpenAI(\n    model_name=\"gpt-3.5-turbo\",\n    temperature=0,\n    max_tokens=2000,\n    model_kwargs = {\n        'frequency_penalty':0,\n        'presence_penalty':0,\n        'top_p':1.0\n    }\n)\n\nschema = Object(\n    id=\"player\",\n    description=(\n        \"User is controlling a music player to select songs, pause or start them or play\"\n        \" music by a particular artist.\"\n    ),\n    attributes=[\n        Text(\n            id=\"song\",\n            description=\"User wants to play this song\",\n            examples=[],\n            many=True,\n        ),\n        Text(\n            id=\"album\",\n            description=\"User wants to play this album\",\n            examples=[],\n            many=True,\n        ),\n        Text(\n            id=\"artist\",\n            description=\"Music by the given artist\",\n            examples=[(\"Songs by paul simon\", \"paul simon\")],\n            many=True,\n        ),\n        Text(\n            id=\"action\",\n            description=\"Action to take one of: `play`, `stop`, `next`, `previous`.\",\n            examples=[\n                (\"Please stop the music\", \"stop\"),\n                (\"play something\", \"play\"),\n                (\"play a song\", \"play\"),\n                (\"next song\", \"next\"),\n            ],\n        ),\n    ],\n    many=False,\n)\n\nchain = create_extraction_chain(llm, schema, encoder_or_encoder_class='json')\nchain.invoke(\"play songs by paul simon and led zeppelin and the doors\")['data']\n```\n\n```python\n{'player': {'artist': ['paul simon', 'led zeppelin', 'the doors']}}\n```\n\n## Pydantic style schema\n\n* See documentation here [pydantic](https://eyurtsev.github.io/kor/validation.html).\n\n```python \nclass Action(enum.Enum):\n    play = \"play\"\n    stop = \"stop\"\n    previous = \"previous\"\n    next_ = \"next\"\n\n\nclass MusicRequest(BaseModel):\n    song: Optional[List[str]] = Field(\n        default=None,\n        description=\"The song(s) that the user would like to be played.\"\n    )\n    album: Optional[List[str]] = Field(\n        default=None,\n        description=\"The album(s) that the user would like to be played.\"\n    )\n    artist: Optional[List[str]] = Field(\n        default=None,\n        description=\"The artist(s) whose music the user would like to hear.\",\n        examples=[(\"Songs by paul simon\", \"paul simon\")],\n    )\n    action: Optional[Action] = Field(\n        default=None,\n        description=\"The action that should be taken; one of `play`, `stop`, `next`, `previous`\",\n        examples=[\n            (\"Please stop the music\", \"stop\"),\n            (\"play something\", \"play\"),\n            (\"play a song\", \"play\"),\n            (\"next song\", \"next\"),\n        ],\n    )\n    \nschema, validator = from_pydantic(MusicRequest)   \nchain = create_extraction_chain(\n    llm, schema, encoder_or_encoder_class=\"json\", validator=validator\n)\nchain.invoke(\"stop the music now\")[\"validated_data\"]\n```\n\n```python\nMusicRequest(song=None, album=None, artist=None, action=\u003cAction.stop: 'stop'\u003e)\n```\n\n\n\n## Compatibility\n\n`Kor` is tested against python 3.8, 3.9, 3.10, 3.11.\n\n## Installation\n\n```sh\npip install kor\n```\n\n## 💡 Ideas\n\nIdeas of some things that could be done with Kor.\n\n* Extract data from text that matches an extraction schema.\n* Power an AI assistant with skills by precisely understanding a user request.\n* Provide natural language access to an existing API.\n\n## 🚧 Prototype\n\nPrototype! So the API is not expected to be stable!\n\n##  ✨ What does Kor excel at?  🌟\n\n* Making mistakes! Plenty of them!\n* Slow! It uses large prompts with examples, and works best with the larger slower LLMs.\n* Crashing for long enough pieces of text! Context length window could become\n  limiting when working with large forms or long text inputs.\n\nThe expectation is that as LLMs improve some of these issues will be mitigated.\n\n## Limitations\n\nKor has no limitations. (Just kidding.)\n\nTake a look at the section above and at the compatibility section.\n\n## Got Ideas?\n\nOpen an issue, and let's discuss!\n\n## 🎶 Why the name?\n\nFast to type and sufficiently unique.\n\n## Contributing\n\nIf you have any ideas or feature requests, please open an issue and share!\n\nSee [CONTRIBUTING.md](https://github.com/eyurtsev/kor/blob/main/CONTRIBUTING.md) for more information.\n\n## Other packages\n\nProbabilistically speaking this package is unlikely to work for your use case.\n\nSo here are some great alternatives:\n\n* [Promptify](https://github.com/promptslab/Promptify)\n* [MiniChain](https://srush.github.io/MiniChain/examples/stats/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feyurtsev%2Fkor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feyurtsev%2Fkor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feyurtsev%2Fkor/lists"}