$git clone https://github.com/microsoft/sammoA flexible, easy-to-use library for running and optimizing prompts for Large Language Models (LLMs).
| 1 | # SAMMO ([📘User Guide](https://microsoft.github.io/sammo/)) |
| 2 | |
| 3 | [](https://pypi.python.org/pypi/sammo) |
| 4 | [](https://opensource.org/licenses/MIT) |
| 5 | [](https://mybinder.org/v2/gh/microsoft/sammo/main?urlpath=tree/docs/tutorials/quickstart.ipynb) |
| 6 | |
| 7 | A flexible, easy-to-use library for running and optimizing prompts for Large Language Models (LLMs). |
| 8 | |
| 9 | ## 🎉 News |
| 10 | - Nov 13, 2024: Turn Markdown into prompt programs: First version of SAMMO express released |
| 11 | - Nov 1, 2024: Use CSS selectors to query and modify prompt programs! |
| 12 | - Oct 15, 2024: SAMMO now supports structured outputs! |
| 13 | |
| 14 | ## How to Get Started |
| 15 | Go to the [user guide](https://microsoft.github.io/sammo/) for examples, how-tos, and API reference. |
| 16 | |
| 17 | Just want to have a quick look? Try the [live demo on Binder](https://mybinder.org/v2/gh/microsoft/sammo/main?urlpath=tree/docs/tutorials/quickstart.ipynb). |
| 18 | |
| 19 | <!--start--> |
| 20 | ### Install library only |
| 21 | |
| 22 | ```bash |
| 23 | pip install sammo |
| 24 | ``` |
| 25 | |
| 26 | ### Install and run tutorials |
| 27 | |
| 28 | ***Prerequisites*** |
| 29 | * Python 3.9+ |
| 30 | |
| 31 | The following commands will install sammo and jupyter and launch jupyter notebook. It's recommended that you create and activate a virtualenv prior to installing packages. |
| 32 | |
| 33 | ```bash |
| 34 | pip install sammo jupyter |
| 35 | |
| 36 | # clone sammo to a local directory |
| 37 | git clone https://github.com/microsoft/sammo.git |
| 38 | cd sammo |
| 39 | |
| 40 | # launch jupyter notebook and open tutorials directory |
| 41 | jupyter notebook --notebook-dir docs/tutorials |
| 42 | ``` |
| 43 | |
| 44 | ## Example |
| 45 | This example shows how easy it is to optimize a prompt with SAMMO. The full example is in the [user guide](https://microsoft.github.io/sammo/). |
| 46 | ```python |
| 47 | runner = OpenAIChat(model_id="gpt-3.5-turbo", api_config=API_CONFIG) |
| 48 | PROMPT_IN_MARKDOWN = """ |
| 49 | # Instructions <!-- #instr --> |
| 50 | Convert the following user queries into a SQL query. |
| 51 | |
| 52 | # Table |
| 53 | Users: |
| 54 | - user_id (INTEGER, PRIMARY KEY) |
| 55 | - name (TEXT) |
| 56 | - age (INTEGER) |
| 57 | - city (TEXT) |
| 58 | |
| 59 | # Complete this |
| 60 | Input: {{{input}}} |
| 61 | Output: |
| 62 | """ |
| 63 | |
| 64 | spp = MarkdownParser(PROMPT_IN_MARKDOWN).get_sammo_program() |
| 65 | mutation_operators = BagOfMutators( |
| 66 | Output(GenerateText(spp)), |
| 67 | Paraphrase("#instr"), |
| 68 | Rewrite("#instr", "Make this more verbose.\n\n {{{{text}}}}") |
| 69 | ) |
| 70 | prompt_optimizer = BeamSearch(runner, mutation_operators, accuracy) |
| 71 | prompt_optimizer.fit(d_train) |
| 72 | prompt_optimizer.show_report() |
| 73 | ``` |
| 74 | |
| 75 | ## Use Cases |
| 76 |  |
| 77 | |
| 78 | SAMMO is designed to support |
| 79 | - **Efficient data labeling**: Supports minibatching by packing and parsing multiple datapoints into a single prompt. |
| 80 | - **Prompt prototyping and engineering**: Re-usable components and prompt structures to quickly build and test new prompts. |
| 81 | - **Instruction optimization**: Optimize instructions to do better on a given task. |
| 82 | - **Prompt compression**: Compress prompts while maintaining performance. |
| 83 | - **Large-scale prompt execution**: parallelization |
| 84 | and rate-limiting out-of-the-box so you can run many queries in parallel and at scale without overwhelming the LLM API. |
| 85 | |
| 86 | It is less useful if you want to build |
| 87 | - Interactive, agent-based LLM applications (→ check out [AutoGen](https://microsoft.github.io/autogen/)) |
| 88 | - Interactive, production-ready LLM applications (→ check out [LangChain](https://www.langchain.com/)) |
| 89 | |
| 90 | <!--end--> |
| 91 | |
| 92 | ## Licence |
| 93 | |
| 94 | This project is licensed under [MIT](https://choosealicense.com/licenses/mit/). |
| 95 | |
| 96 | To cite this paper, you can use the following BibTeX entry: |
| 97 | |
| 98 | ```bibtex |
| 99 | @inproceedings{schnabel-neville-2024-symbolic, |
| 100 | title = "Symbolic Prompt Program Search: A Structure-Aware Approach to Efficient Compile-Time Prompt Optimization", |
| 101 | author = "Schnabel, Tobias and Neville, Jennifer", |
| 102 | booktitle = "Findings of the Association for Computational Linguistics: EMNLP 2024", |
| 103 | year = "2024", |
| 104 | url = "https://aclanthology.org/2024.findings-emnlp.37", |
| 105 | pages = "670--686" |
| 106 | } |
| 107 | ``` |
| 108 | |
| 109 | ## Authors |
| 110 | |
| 111 | `SAMMO` was written by [Tobias Schnabel](ma |