Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make model generation prompts more flexible #69

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/draive/generation/model/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
]


async def generate_model[Generated: Model](
async def generate_model[Generated: Model]( # noqa: PLR0913
generated: type[Generated],
/,
*,
instruction: str,
input: MultimodalContent, # noqa: A002
schema_variable: str | None = None,
tools: Toolbox | None = None,
examples: Iterable[tuple[MultimodalContent, Generated]] | None = None,
**extra: Any,
Expand All @@ -26,6 +27,7 @@ async def generate_model[Generated: Model](
generated,
instruction=instruction,
input=input,
schema_variable=schema_variable,
tools=tools or model_generation.tools,
examples=examples,
**extra,
Expand Down
1 change: 1 addition & 0 deletions src/draive/generation/model/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async def __call__[Generated: Model]( # noqa: PLR0913
*,
instruction: str,
input: MultimodalContent, # noqa: A002
schema_variable: str | None = None,
tools: Toolbox | None = None,
examples: Iterable[tuple[MultimodalContent, Generated]] | None = None,
**extra: Any,
Expand Down
30 changes: 20 additions & 10 deletions src/draive/generation/model/lmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,33 @@
]


async def lmm_generate_model[Generated: Model](
async def lmm_generate_model[Generated: Model]( # noqa: PLR0913
generated: type[Generated],
/,
*,
instruction: str,
input: MultimodalContent, # noqa: A002
schema_variable: str | None = None,
tools: Toolbox | None = None,
examples: Iterable[tuple[MultimodalContent, Generated]] | None = None,
**extra: Any,
) -> Generated:
system_message: LMMMessage = LMMMessage(
role="system",
content=INSTRUCTION.format(
instruction=instruction,
format=generated.specification(),
),
)
system_message: LMMMessage
if variable := schema_variable:
system_message = LMMMessage(
role="system",
content=instruction.format(**{variable: generated.specification()}),
)

else:
system_message = LMMMessage(
role="system",
content=DEFAULT_INSTRUCTION.format(
instruction=instruction,
schema=generated.specification(),
),
)

input_message: LMMMessage = LMMMessage(
role="user",
content=input,
Expand Down Expand Up @@ -71,13 +81,13 @@ async def lmm_generate_model[Generated: Model](
return generated.from_json(completion.content_string)


INSTRUCTION: str = """\
DEFAULT_INSTRUCTION: str = """\
{instruction}

IMPORTANT!
The result have to conform to the following JSON Schema:
```
{format}
{schema}
```
Provide ONLY a single, raw, valid JSON without any comments or formatting.
"""
18 changes: 8 additions & 10 deletions src/draive/helpers/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,17 @@ def setup_logging(
},
},
"loggers": {
"": { # root logger
name: {
"handlers": ["console"],
"level": "DEBUG" if debug else "INFO",
"propagate": False,
},
**{
name: {
"handlers": ["console"],
"level": "DEBUG" if debug else "INFO",
"propagate": False,
}
for name in loggers
},
}
for name in loggers
},
"root": { # root logger
"handlers": ["console"],
"level": "DEBUG" if debug else "INFO",
"propagate": False,
},
},
)
Loading