-
Notifications
You must be signed in to change notification settings - Fork 9
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
Feat/llm responses #376
base: dev
Are you sure you want to change the base?
Feat/llm responses #376
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears this PR is a release PR (change its base from master
if that is not the case).
Here's a release checklist:
- Update package version
- Update
poetry.lock
- Change PR merge option
- Update template repo
- Search for objects to be deprecated
… type annotations
I got an idea for more complex prompts: we can allow passing responses as prompts instead of just strings. And then it'd be possible to incorporate slots into a prompt: model = LLM_API(prompt=rsp.slots.FilledTemplate("You are an experienced barista in a local coffeshop."
"Answer your customers questions about coffee and barista work.\n"
"Customer data:\nAge {person.age}\nGender: {person.gender}\nFavorite drink: {person.habits.drink}"
)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've marked all resolved conversations as resolved (don't forget to put correct commit hash!).
There are still 25 unresolved conversations.
I've edited 9 of them with PROMPT REWORK
or POSTPONED
prefixes: the first ones are for me to resolve, the latter -- to be resolved in a later PR.
Please respond to the other 16 comments (plus the ones from this review): either with a commit hash of a commit that resolves it or with your comments regarding the suggestion.
chatsky/llm/filters.py
Outdated
raise NotImplemented | ||
|
||
def __call__(self, ctx, request, response, model_name): | ||
return self.call(ctx, request, model_name) + self.call(ctx, response, model_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to be bitwise or:
return self.call(ctx, request, model_name) + self.call(ctx, response, model_name) | |
return self.call(ctx, request, model_name) | self.call(ctx, response, model_name) |
Add tests. They did not catch this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chatsky/llm/filters.py
Outdated
if request is not None and request.misc is not None and request.misc.get("important", None): | ||
return self.Return.Request | ||
if response is not None and response.misc is not None and response.misc.get("important", None): | ||
return self.Return.Response |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If both contain "important" this will return Request
instead of Turn
.
Implement this as MessageFilter
.
Same for FromModel
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chatsky/slots/llm.py
Outdated
} | ||
return ExtractedGroupSlot(**res) | ||
|
||
def __flatten_llm_group_slot(self, slot, parent_key=""): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You missed at least one in
if isinstance(value, LLMGroupSlot):
items.update(self.__flatten_llm_group_slot(value, new_key))
Add tests that use nested LLMGroupSlots.
chatsky/llm/llm_api.py
Outdated
raise ValueError | ||
|
||
async def condition( | ||
self, ctx: Context, prompt: str, method: BaseMethod, return_schema: Optional[BaseModel] = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does condition not support context history?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chatsky/llm/llm_api.py
Outdated
result.annotations = {"__generated_by_model__": self.name} | ||
return result | ||
|
||
async def condition(self, prompt: str, method: BaseMethod, return_schema=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it not possible to use message schema with log probs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some lines are clearly not covered by the tests:
Slots are not tested at all.
Run poe quick_test_coverage
to generate html reports in htmlcov
directory.
You can then view them (by opening htmlcov/index.html
) to see which lines are not covered.
tutorials/llm/1_basics.py
Outdated
it will be reused across all the nodes and therefore it will store all dialogue history. | ||
This is not advised if you are short on tokens or if you do not need to store all dialogue history. | ||
Alternatively you can instantiate model object inside of RESPONSE field in the nodes you need. | ||
Via `history` parameter you can set number of dialogue _turns_ that the model will use as the history. Default value is `5`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is out of place.
This should be in filtering_history
or as a comment near the line where LLMResponse
is initialized with history=0
.
tutorials/llm/5_llm_slots.py
Outdated
}, | ||
"tell": { | ||
RESPONSE: rsp.FilledTemplate( | ||
"So you are {person.username} and your occupation is {person.job}, right?" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
person
group slot does not allow partial extraction.
Add the flag, mention it, link to partial extraction tutorial.
…ited from MessageFilter
…ter initialization
…gue pairs and reduce code redundancy
|
||
# %% [markdown] | ||
""" | ||
Chatsky enables you to use more complex prompts then a simple string if need be. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chatsky enables you to use more complex prompts then a simple string if need be. | |
Chatsky enables you to use more complex prompts than a simple string if needed. |
""" | ||
Chatsky enables you to use more complex prompts then a simple string if need be. | ||
In this example we create a VacantPlaces class, that can dynamically retrieve | ||
some external data and put them into the prompt. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some external data and put them into the prompt. | |
some external data and put it into the prompt. |
toy_script = { | ||
GLOBAL: { | ||
MISC: { | ||
# this prompt will be overwritten with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# this prompt will be overwritten with | |
# this prompt will be overwritten in |
GLOBAL: { | ||
MISC: { | ||
# this prompt will be overwritten with | ||
# every node with `prompt` key in it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# every node with `prompt` key in it | |
# every node by the `prompt` key in it |
|
||
# %% [markdown] | ||
""" | ||
Let's create a simple script to demonstrate this. Note, that prompts should go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's create a simple script to demonstrate this. Note, that prompts should go | |
Let's create a simple script to demonstrate this. Note that prompts should go into |
Inside `MISC` they can be stored under the `prompt`, | ||
`global_prompt` or `local_prompt` keys. | ||
Please note, that `MISC` is just a dictionary and its fields can be overwritten | ||
in any node, if given the same key. You can utilize this in your project, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in any node, if given the same key. You can utilize this in your project, |
`global_prompt` or `local_prompt` keys. | ||
Please note, that `MISC` is just a dictionary and its fields can be overwritten | ||
in any node, if given the same key. You can utilize this in your project, | ||
but below we present the intended way of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but below we present the intended way of | |
Below we present the intended way of |
If you want to take the messages that meet your particular criteria and pass | ||
them to the LLMs context you can use the `LLMResponse`s `filter_func` parameter. | ||
It must be a function that takes a single `Message` | ||
object and returns a boolean. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be an old description. Shouldn't it be like this?
It must be a class, whose call method takes the following args:
context
,request
,response
, andmodel_name
@@ -75,6 +75,7 @@ opentelemetry-instrumentation = { version = "*", optional = true } | |||
sqlalchemy = { version = "*", extras = ["asyncio"], optional = true } | |||
opentelemetry-exporter-otlp = { version = ">=1.20.0", optional = true } # log body serialization is required | |||
pyyaml = { version = "*", optional = true } | |||
langchain = { version = "*", optional = true } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about langchain-openai
? it doesn't seem to be installed by default and I got import errors until I added it manually
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error
INFO:chatsky.core.script_parsing:Loaded file /home/rami1996/ui/my_project/bot/scripts/build.yaml
INFO:chatsky.messengers.common.interface:Attachments directory for LongpollingInterface messenger interface is None, so will be set to tempdir and attachment data won't be cached locally!
Exception: [ModuleNotFoundError("No module named 'langchain_openai'")]
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/rami1996/ui/my_project/./app.py", line 19, in <module>
main()
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/click/core.py", line 1161, in __call__
return self.main(*args, **kwargs)
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/click/core.py", line 1082, in main
rv = self.invoke(ctx)
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/click/core.py", line 1443, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/click/core.py", line 788, in invoke
return __callback(*args, **kwargs)
File "/home/rami1996/ui/my_project/./app.py", line 14, in main
pipeline = Pipeline.from_file(file = script_path, custom_dir = Path(script_path).parent.parent / "custom")
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/chatsky/core/pipeline.py", line 182, in from_file
pipeline = JSONImporter(custom_dir=custom_dir).import_pipeline_file(file)
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/chatsky/core/script_parsing.py", line 263, in import_pipeline_file
return self.replace_resolvable_objects(pipeline)
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/chatsky/core/script_parsing.py", line 232, in replace_resolvable_objects
return {k: (self.replace_resolvable_objects(v)) for k, v in obj.items()}
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/chatsky/core/script_parsing.py", line 232, in <dictcomp>
return {k: (self.replace_resolvable_objects(v)) for k, v in obj.items()}
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/chatsky/core/script_parsing.py", line 232, in replace_resolvable_objects
return {k: (self.replace_resolvable_objects(v)) for k, v in obj.items()}
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/chatsky/core/script_parsing.py", line 232, in <dictcomp>
return {k: (self.replace_resolvable_objects(v)) for k, v in obj.items()}
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/chatsky/core/script_parsing.py", line 229, in replace_resolvable_objects
args, kwargs = self.parse_args(obj[key])
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/chatsky/core/script_parsing.py", line 196, in parse_args
value = self.replace_resolvable_objects(value)
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/chatsky/core/script_parsing.py", line 232, in replace_resolvable_objects
return {k: (self.replace_resolvable_objects(v)) for k, v in obj.items()}
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/chatsky/core/script_parsing.py", line 232, in <dictcomp>
return {k: (self.replace_resolvable_objects(v)) for k, v in obj.items()}
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/chatsky/core/script_parsing.py", line 230, in replace_resolvable_objects
return self.resolve_string_reference(key)(*args, **kwargs)
File "/home/rami1996/.cache/pypoetry/virtualenvs/chatsky-ui-Yhfmz2Co-py3.10/lib/python3.10/site-packages/chatsky/core/script_parsing.py", line 181, in resolve_string_reference
raise JSONImportError(f"Could not import {obj}") from Exception(exceptions)
chatsky.core.script_parsing.JSONImportError: Could not import langchain_openai.ChatOpenAI
Read the guide on Pipeline import from file: https://deeppavlov.github.io/chatsky/user_guides/pipeline_import.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not include langchain-<provider>
packages by default to prevent dependencies versions mismatch. As it is shown in tutorials you need to install them by yourself like so pip install chatsky[llm] langchain-openai
especially when using smaller models. | ||
|
||
Note that we are passing the name of the model | ||
from pipeline.models dictionary to LLMGroupSlot.model field. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't get it! pipeline.models
and LLMGrouptSlot.model
seem not to be mentioned elsewhere
Description
Added functionality for calling LLMs via langchain API for utilizing them in responses and conditions.
Checklist
List here tasks to complete in order to mark this PR as ready for review.
To Consider