{"id":15442504,"url":"https://github.com/florianmgs/ask-llm","last_synced_at":"2025-04-19T18:33:06.692Z","repository":{"id":225927735,"uuid":"767042355","full_name":"FlorianMgs/ask-llm","owner":"FlorianMgs","description":"The easiest way to supercharge your apps with LLM!","archived":false,"fork":false,"pushed_at":"2024-10-22T07:38:00.000Z","size":35,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T19:50:03.884Z","etag":null,"topics":["ai","decorators","jinja","langchain","langchain-decorators","lcel","llm","mistral","openai","python","templates"],"latest_commit_sha":null,"homepage":"","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/FlorianMgs.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":"2024-03-04T15:48:38.000Z","updated_at":"2025-01-14T15:58:31.000Z","dependencies_parsed_at":"2024-03-05T02:44:37.643Z","dependency_job_id":"d8bfc629-4bd6-41be-b656-157112b137f6","html_url":"https://github.com/FlorianMgs/ask-llm","commit_stats":{"total_commits":19,"total_committers":3,"mean_commits":6.333333333333333,"dds":"0.21052631578947367","last_synced_commit":"120cc62f24c86c1d6635467c21c81b6b2055d68d"},"previous_names":["florianmgs/ask-ai","florianmgs/ask-llm"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FlorianMgs%2Fask-llm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FlorianMgs%2Fask-llm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FlorianMgs%2Fask-llm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FlorianMgs%2Fask-llm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FlorianMgs","download_url":"https://codeload.github.com/FlorianMgs/ask-llm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249765230,"owners_count":21322387,"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":["ai","decorators","jinja","langchain","langchain-decorators","lcel","llm","mistral","openai","python","templates"],"created_at":"2024-10-01T19:28:11.678Z","updated_at":"2025-04-19T18:33:06.659Z","avatar_url":"https://github.com/FlorianMgs.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## ask-llm: the easiest way to supercharge your apps with LLM!\nask-llm is a very simple yet powerful package that can turn anything into a LLM interaction.  \nYou just need to decorate a function with `@ask()`, write a prompt into the docstring, give it a return type, and there you go, you got your LLM interaction.  \nThis takes inspiration from the awesome langchain-decorators package.  \nIt works out of the box with OpenAI and Anthropic (by setting a `OPENAI_API_KEY` or `ANTHROPIC_API_KEY` env var), but is compatible with all `BaseChatModel` from Langchain.\n\n## Features\n- Write your prompts in docstrings using Jinja templating language. \n- Access your function args in your prompt.  \n- (Almost) full access in your prompt to every attributes / properties of objects as soon as it's not a callable.  \n- Conversation support\n- Fully compatible with Langchain / LCEL. Returns either the LLM response, the formatted `ChatPromptTemplate` or a chain `prompt | llm`.  \n- Access the decorated function return value inside your prompt. Use `{{ __result__ }}` anywhere in your prompt.  \n- Format the LLM answer using Pydantic objects as return type, or regular python return types.  \n- If passed a Pydantic object as return type, LLM will retry if it fails to answer on the first shot.  \n- If using GPT Vision, supports an `image` parameter to send alongside your prompt. You can input your image either as an url, a path, or a base64 string.  \nand many more to come...\n\n### Installation\nEither using `pip` or by cloning this repo. \n`pip install ask-llm`\n\nThen, import the decorator:\n`from ask_llm import ask`\n\nTo make it work out of the box with OpenAI, define an env var:\n`OPENAI_API_KEY=\u003cyour key\u003e`\n\nNow, you are able to use the decorator:\n```python\nimport requests\nfrom functools import cached_property\nfrom ask_llm import ask\nfrom pydantic import BaseModel\n\n\nclass BlogArticle(BaseModel):\n    title: str\n    content: str\n\n\nclass WikipediaAPI:\n    def __init__(self, title: str):\n        self.title = title\n\n    @cached_property\n    def wikipedia_article(self) -\u003e str | None:\n        try:\n            response = requests.get(\n                f\"https://en.wikipedia.org/api/rest_v1/page/summary/{self.title}\"\n            )\n            return response.json()[\"extract\"]\n        except:\n            return\n\n\nclass BlogArticleWriter:\n    def __init__(\n        self,\n        title: str,\n        keywords: list,\n        nb_headings: str,\n        nb_paragraphs: str,\n        input_bulletpoints: bool,\n    ):\n        self.title = title\n        self.keywords = keywords\n        self.nb_headings = nb_headings\n        self.nb_paragraphs = nb_paragraphs\n        self.input_bulletpoints = input_bulletpoints\n\n        self.wikipedia_api = WikipediaAPI(title=self.title)\n\n    @ask()\n    def write_blog_article(self, author: str) -\u003e BlogArticle:\n        \"\"\"\n        As an expert copywriter specialized in SEO and content writing, your task is to write a very informative blog article\n        about the topic {{ self.title }}.\n        You should create {{ self.nb_headings }} highly engaging headings made of {{ self.nb_paragraphs }} paragraphs each.\n        Use subheadings and line breaks when appropriate.\n\n        {% if self.input_bulletpoints %}\n          You should also include bulletpoints in the article.\n        {% endif %}\n\n        {% if self.wikipedia_api.wikipedia_article %}\n          Here is a brief summary of the topic:\n          {{ self.wikipedia_api.wikipedia_article }}\n        {% endif %}\n\n        {% if self.keywords %}\n          The following keywords should be included in the article:\n          {% for keyword in self.keywords %}\n            - {{ keyword }}\n          {% endfor %}\n        {% endif %}\n\n        The article should be written by {{ author }}.\n        \"\"\"\n\nwriter = BlogArticleWriter(\n    title=\"Large language models\", \n    keywords=[\"llm\", \"open source\", \"python\", \"nlp\"], \n    nb_headings=3, \n    nb_paragraphs=3, \n    input_bulletpoints=True\n)\n\nblog_article = writer.write_blog_article(\"Florian\")\n```\nLangsmith trace: https://smith.langchain.com/public/0abe7b97-d43e-4c10-9bba-be9f6c2892d6/r\n\n\nAn other example, with an image:\n```python\nfrom ask_llm import ask\n\n\n@ask()\ndef describe_image(image: str) -\u003e str:\n    \"\"\"\n    Describe this image precisely.\n    \"\"\"\n\n\ndescription = describe_image(image=\"https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Tour_Eiffel_Wikimedia_Commons_%28cropped%29.jpg/800px-Tour_Eiffel_Wikimedia_Commons_%28cropped%29.jpg\")\n```\nLangsmith trace: https://smith.langchain.com/public/18f39957-93f5-47f2-a264-051c11cca2e8/r\n\n\nConversation example:\n```python\nfrom ask_llm import ask\n\n\n@ask()\ndef conversation(instruction: str) -\u003e str:\n    \"\"\"\n    {% chat %}\n        {% message system %}\n            You are an helpful assistant that can answer all the user questions.\n        {% endmessage %}\n        {% message ai %}\n            Hello, Arnaud! How can I help you today?\n        {% endmessage %}\n        {% message human %}\n            {{ instruction }}\n        {% endmessage %}\n    {% endchat %}\n    Answer in CAPS LOCK\n    \"\"\"\n```\n\n\n### Settings  \nYou can pass numerous arguments to the decorator:  \n- `call: bool = True` To call the LLM or to return the prepared chain `prompt | llm`  \n- `verbose: bool = False` To enable verbose mode  \n- `return_prompt_only: bool = False` To return only the formatted `ChatPromptTemplate`  \n- `model_name: str = \"gpt-4-vision-preview\"` The model name  \n- `max_tokens: int = 4096` max tokens for the answer  \n- `image_quality: str = \"high\"` If passed an image, the quality parameter for Vision API  \n- `chat_model_class: BaseChatModel = ChatOpenAI` The Langchain subclass of `BaseChatModel` to use.  \n- `**llm_kwargs` Other kwargs to pass to the `BaseChatModel` if any.  \n\n\n### Conclusion\nPossibilities with this are endless. Hope you're gonna like it!  \nMore features and examples are coming.  \nSpecial integration with Django is also coming.  \nDo not hesitate to iterate and contribute to the project! Please submit PRs and issues 🙏\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflorianmgs%2Fask-llm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflorianmgs%2Fask-llm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflorianmgs%2Fask-llm/lists"}