Intel
Creating Intel
In Dandy we use an object called Intel to represent the information about objects in your project.
Let's go ahead and create a ClownIntel class that inherits from the BaseIntel class.
from dandy import BaseIntel
class ClownIntel(BaseIntel):
clown_name: str
can_juggle: bool
real_name: str | None = None
bozo = ClownIntel(
clown_name='Bozo',
can_juggle=True
)
print(bozo.can_juggle)
True
Note
On the class ClownIntel we may not know the real name of the clown, which is why we set real_name to be optional.
This is important as all Intel objects are type validated and would raise an error if the real_name were not set.
Using Intel
Now that we have a ClownIntel object, let's use it with the LlmBot to generate a new clown.
The process method of the LlmBot will return an Intel object based on the intel_class argument.
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.11/site-packages/markdown_exec/_internal/formatters/python.py", line 89, in _run_python
exec_python(code, code_block_id, exec_globals)
File "/app/.heroku/python/lib/python3.11/site-packages/markdown_exec/_internal/formatters/_exec_python.py", line 26, in exec_python
exec(compiled, exec_globals) # noqa: S102
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<code block: session intel; n2>", line 5, in <module>
new_clown = Bot().process(
^^^^^^^^^^^^^^
File "/workspace/dandy/bot/bot.py", line 79, in process
return self.llm.prompt_to_intel(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/service.py", line 67, in prompt_to_intel
return self._llm_connector.prompt_to_intel(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/connector.py", line 136, in prompt_to_intel
response_intel_object = self._request_to_intel()
^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/connector.py", line 156, in _request_to_intel
self._http_request_to_response_str()
File "/workspace/dandy/llm/connector.py", line 66, in _http_request_to_response_str
self.response_str = http_connector.request_to_response(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/http/connector.py", line 37, in request_to_response
raise HttpConnectorRecoverableError(message)
dandy.http.exceptions.HttpConnectorRecoverableError: HttpConnector request failed with status code 400 and the following message "Bad Request" and body text "{"error":{"message":"/chat/completions: Invalid model name passed in model=stratus.smart. Call `/v1/models` to view available models for your key.","type":"None","param":"None","code":"400","provider_specific_fields":{"error":"/chat/completions: Invalid model name passed in model=stratus.smart. Call `/v1/models` to view available models for your key."}}}" after 4 attempts
List Intel
Let's say we want to create a list of ClownIntel objects and still have all the support of the BaseIntel class.
We can use the BaseListIntel class to do that.
# Using ClownIntel from earlier
from dandy import BaseListIntel
class ClownsIntel(BaseListIntel):
clowns: list[ClownIntel]
clowns_intel = ClownsIntel(clowns=[
ClownIntel(clown_name='Bozo', can_juggle=True),
ClownIntel(clown_name='Bimbo', can_juggle=False),
ClownIntel(clown_name='Bongo', can_juggle=True),
])
clowns_intel.append(ClownIntel(clown_name='Bubba', can_juggle=True))
print(len(clowns_intel))
for clown_intel in clowns_intel:
print(clown_intel)
4 clown_name='Bozo' can_juggle=True real_name=None clown_name='Bimbo' can_juggle=False real_name=None clown_name='Bongo' can_juggle=True real_name=None clown_name='Bubba' can_juggle=True real_name=None
Include and Exclude Fields
In the process method of the LlmBot class we can now include and exclude fields from the Intel object.
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.11/site-packages/markdown_exec/_internal/formatters/python.py", line 89, in _run_python
exec_python(code, code_block_id, exec_globals)
File "/app/.heroku/python/lib/python3.11/site-packages/markdown_exec/_internal/formatters/_exec_python.py", line 26, in exec_python
exec(compiled, exec_globals) # noqa: S102
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<code block: session intel; n4>", line 12, in <module>
another_clown = FakeClownBot().process(
^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/bot/recorder.py", line 57, in wrapper
result = method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^
File "<code block: session intel; n4>", line 5, in process
return self.llm.prompt_to_intel(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/service.py", line 67, in prompt_to_intel
return self._llm_connector.prompt_to_intel(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/connector.py", line 136, in prompt_to_intel
response_intel_object = self._request_to_intel()
^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/connector.py", line 156, in _request_to_intel
self._http_request_to_response_str()
File "/workspace/dandy/llm/connector.py", line 66, in _http_request_to_response_str
self.response_str = http_connector.request_to_response(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/http/connector.py", line 37, in request_to_response
raise HttpConnectorRecoverableError(message)
dandy.http.exceptions.HttpConnectorRecoverableError: HttpConnector request failed with status code 400 and the following message "Bad Request" and body text "{"error":{"message":"/chat/completions: Invalid model name passed in model=stratus.smart. Call `/v1/models` to view available models for your key.","type":"None","param":"None","code":"400","provider_specific_fields":{"error":"/chat/completions: Invalid model name passed in model=stratus.smart. Call `/v1/models` to view available models for your key."}}}" after 4 attempts
You can also get the same result by including both the can_juggle and clown_name fields.
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.11/site-packages/markdown_exec/_internal/formatters/python.py", line 89, in _run_python
exec_python(code, code_block_id, exec_globals)
File "/app/.heroku/python/lib/python3.11/site-packages/markdown_exec/_internal/formatters/_exec_python.py", line 26, in exec_python
exec(compiled, exec_globals) # noqa: S102
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<code block: session intel; n5>", line 12, in <module>
another_clown = RealClownBot().process(
^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/bot/recorder.py", line 57, in wrapper
result = method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^
File "<code block: session intel; n5>", line 5, in process
return self.llm.prompt_to_intel(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/service.py", line 67, in prompt_to_intel
return self._llm_connector.prompt_to_intel(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/connector.py", line 136, in prompt_to_intel
response_intel_object = self._request_to_intel()
^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/connector.py", line 156, in _request_to_intel
self._http_request_to_response_str()
File "/workspace/dandy/llm/connector.py", line 66, in _http_request_to_response_str
self.response_str = http_connector.request_to_response(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/http/connector.py", line 37, in request_to_response
raise HttpConnectorRecoverableError(message)
dandy.http.exceptions.HttpConnectorRecoverableError: HttpConnector request failed with status code 400 and the following message "Bad Request" and body text "{"error":{"message":"/chat/completions: Invalid model name passed in model=stratus.smart. Call `/v1/models` to view available models for your key.","type":"None","param":"None","code":"400","provider_specific_fields":{"error":"/chat/completions: Invalid model name passed in model=stratus.smart. Call `/v1/models` to view available models for your key."}}}" after 4 attempts
Warning
All non-optional/required fields must be included either through excluding optional fields or manually including required fields.
Advanced Intel
The Intel class is designed to provide lots of customization and control over your data.
Take a look at the next ...
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.11/site-packages/markdown_exec/_internal/formatters/python.py", line 89, in _run_python
exec_python(code, code_block_id, exec_globals)
File "/app/.heroku/python/lib/python3.11/site-packages/markdown_exec/_internal/formatters/_exec_python.py", line 26, in exec_python
exec(compiled, exec_globals) # noqa: S102
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<code block: session intel; n6>", line 29, in <module>
new_pirate = Bot().process(
^^^^^^^^^^^^^^
File "/workspace/dandy/bot/bot.py", line 79, in process
return self.llm.prompt_to_intel(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/service.py", line 67, in prompt_to_intel
return self._llm_connector.prompt_to_intel(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/connector.py", line 136, in prompt_to_intel
response_intel_object = self._request_to_intel()
^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/connector.py", line 156, in _request_to_intel
self._http_request_to_response_str()
File "/workspace/dandy/llm/connector.py", line 66, in _http_request_to_response_str
self.response_str = http_connector.request_to_response(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/http/connector.py", line 37, in request_to_response
raise HttpConnectorRecoverableError(message)
dandy.http.exceptions.HttpConnectorRecoverableError: HttpConnector request failed with status code 400 and the following message "Bad Request" and body text "{"error":{"message":"/chat/completions: Invalid model name passed in model=stratus.smart. Call `/v1/models` to view available models for your key.","type":"None","param":"None","code":"400","provider_specific_fields":{"error":"/chat/completions: Invalid model name passed in model=stratus.smart. Call `/v1/models` to view available models for your key."}}}" after 4 attempts
Advanced Include and Exclude Fields
Now that we have a more complex PirateIntel object, let's see how we can include and exclude fields while still getting a valid PirateIntel object.
We do this by creating a more complex IncEx/Dict object where the keys are the field names and the values are either True or a Dict of that child objects field names.
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.11/site-packages/markdown_exec/_internal/formatters/python.py", line 89, in _run_python
exec_python(code, code_block_id, exec_globals)
File "/app/.heroku/python/lib/python3.11/site-packages/markdown_exec/_internal/formatters/_exec_python.py", line 26, in exec_python
exec(compiled, exec_globals) # noqa: S102
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<code block: session intel; n7>", line 12, in <module>
new_pirate = AdvancedPirateBot().process(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/bot/recorder.py", line 57, in wrapper
result = method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^
File "<code block: session intel; n7>", line 5, in process
return self.llm.prompt_to_intel(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/service.py", line 67, in prompt_to_intel
return self._llm_connector.prompt_to_intel(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/connector.py", line 136, in prompt_to_intel
response_intel_object = self._request_to_intel()
^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/llm/connector.py", line 156, in _request_to_intel
self._http_request_to_response_str()
File "/workspace/dandy/llm/connector.py", line 66, in _http_request_to_response_str
self.response_str = http_connector.request_to_response(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/dandy/http/connector.py", line 37, in request_to_response
raise HttpConnectorRecoverableError(message)
dandy.http.exceptions.HttpConnectorRecoverableError: HttpConnector request failed with status code 400 and the following message "Bad Request" and body text "{"error":{"message":"/chat/completions: Invalid model name passed in model=stratus.smart. Call `/v1/models` to view available models for your key.","type":"None","param":"None","code":"400","provider_specific_fields":{"error":"/chat/completions: Invalid model name passed in model=stratus.smart. Call `/v1/models` to view available models for your key."}}}" after 4 attempts