|
| 1 | +# How-To: Define few shots |
| 2 | + |
| 3 | +There are many ways to improve the accuracy of IQL generation - one of them is to use few-shot prompting. db-ally allows you to inject few-shot examples for any type of defined view, both structured and freeform. |
| 4 | + |
| 5 | +Few shots are defined in the [`list_few_shots`](../../reference/views/index.md#dbally.views.base.BaseView.list_few_shots) method, each few shot example should be an instance of [`FewShotExample`](../../reference/prompt.md#dbally.prompt.elements.FewShotExample) class that defines example question and expected LLM answer. |
| 6 | + |
| 7 | +## Structured views |
| 8 | + |
| 9 | +For structured views, both questions and answers for [`FewShotExample`](../../reference/prompt.md#dbally.prompt.elements.FewShotExample) can be defined as a strings, whereas in case of answers Python expressions are also allowed (please see lambda function in example below). |
| 10 | + |
| 11 | +```python |
| 12 | +from dbally.prompt.elements import FewShotExample |
| 13 | +from dbally.views.sqlalchemy_base import SqlAlchemyBaseView |
| 14 | + |
| 15 | +class RecruitmentView(SqlAlchemyBaseView): |
| 16 | + """ |
| 17 | + A view for retrieving candidates from the database. |
| 18 | + """ |
| 19 | + |
| 20 | + def list_few_shots(self) -> List[FewShotExample]: |
| 21 | + return [ |
| 22 | + FewShotExample( |
| 23 | + "Which candidates studied at University of Toronto?", |
| 24 | + 'studied_at("University of Toronto")', |
| 25 | + ), |
| 26 | + FewShotExample( |
| 27 | + "Do we have any soon available perfect fits for senior data scientist positions?", |
| 28 | + lambda: ( |
| 29 | + self.is_available_within_months(1) |
| 30 | + and self.data_scientist_position() |
| 31 | + and self.has_seniority("senior") |
| 32 | + ), |
| 33 | + ), |
| 34 | + ... |
| 35 | + ] |
| 36 | +``` |
| 37 | + |
| 38 | +## Freeform views |
| 39 | + |
| 40 | +Currently freeform views accept SQL query syntax as a raw string. The larger variety of passing parameters is considered to be implemented in further db-ally releases. |
| 41 | + |
| 42 | +```python |
| 43 | +from dbally.prompt.elements import FewShotExample |
| 44 | +from dbally.views.freeform.text2sql import BaseText2SQLView |
| 45 | + |
| 46 | +class RecruitmentView(BaseText2SQLView): |
| 47 | + """ |
| 48 | + A view for retrieving candidates from the database. |
| 49 | + """ |
| 50 | + |
| 51 | + def list_few_shots(self) -> List[FewShotExample]: |
| 52 | + return [ |
| 53 | + FewShotExample( |
| 54 | + "Which candidates studied at University of Toronto?", |
| 55 | + 'SELECT name FROM candidates WHERE university = "University of Toronto"', |
| 56 | + ), |
| 57 | + FewShotExample( |
| 58 | + "Which clients are from NY?", |
| 59 | + 'SELECT name FROM clients WHERE city = "NY"', |
| 60 | + ), |
| 61 | + ... |
| 62 | + ] |
| 63 | +``` |
| 64 | + |
| 65 | +## Prompt format |
| 66 | + |
| 67 | +By default each few shot is injected subsequent to a system prompt message. The format is as follows: |
| 68 | + |
| 69 | +```python |
| 70 | +[ |
| 71 | + { |
| 72 | + "role" "user", |
| 73 | + "content": "Question", |
| 74 | + }, |
| 75 | + { |
| 76 | + "role": "assistant", |
| 77 | + "content": "Answer", |
| 78 | + } |
| 79 | +] |
| 80 | +``` |
| 81 | + |
| 82 | +If you use `examples` formatting tag in content field of the system or user message, all examples are going to be injected inside the message without additional conversation. |
| 83 | + |
| 84 | +The example of prompt utilizing `examples` tag: |
| 85 | + |
| 86 | +```python |
| 87 | +[ |
| 88 | + { |
| 89 | + "role" "system", |
| 90 | + "content": "Here are example resonses:\n {examples}", |
| 91 | + }, |
| 92 | +] |
| 93 | +``` |
| 94 | + |
| 95 | +!!!info |
| 96 | + There is no best way to inject a few shot example. Different models can behave diffrently based on few shots formatting of choice. |
| 97 | + Generally, first appoach should yield the best results in most cases. Therefore, adding example tags in your custom prompts is not recommended. |
0 commit comments