Skip to content

Commit

Permalink
feat: use end_of_turn to end responses
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed Aug 23, 2024
1 parent b4fdb9d commit c873c50
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 10 deletions.
16 changes: 10 additions & 6 deletions reasonify-headless/reasonify/core/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..utils.context import Context, new_checkpoint
from ..utils.inject import dispatch_context
from ..utils.queue import QueueWrapper
from ..utils.run import get_context, run
from ..utils.run import run
from ..utils.serialize import json
from ..utils.tool import tool

Expand All @@ -21,13 +21,17 @@ async def intro(c: Context, query: str, messages: list[Message]):
messages.append(user > query)

response = c["response"] = []
c["end"] = False

@tool
def reply(message: str):
def reply(*messages: str):
"""talk to the user"""
response.append(message)
response.extend(messages)

get_context()["reply"] = reply
@tool
def end_of_turn():
"""end this round of conversation and pass the microphone to the user"""
c["end"] = True


@intro.pre_process
Expand Down Expand Up @@ -77,15 +81,15 @@ def mid_process(self, c: Context, response: list[str]):
self._index += 1

@dispatch_context
async def end_process(self, messages: list[Message], response: list, sources: list):
async def end_process(self, messages: list[Message], end: bool, sources: list):
self._queue.end()
await self.future

if sources:
messages.append(assistant > json(sources))
messages.append(system @ "results" > json(self.results))

if not response:
if not end:
return # next round

raise Jump(out_of=main_loop) # already responded or nothing generated
2 changes: 2 additions & 0 deletions reasonify-headless/reasonify/examples/async.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
print(i)
reply("Do you want to know more about async generators?")
end_of_turn()
- Yes! How can I `yield from` an async generator?

Expand All @@ -45,3 +46,4 @@
await g()
reply("This will print 1, 2, 3.")
end_of_turn()
1 change: 1 addition & 0 deletions reasonify-headless/reasonify/examples/date.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
- source: |
import datetime
datetime.datetime.now().strftime('%Y-%m-%d')
end_of_turn()
2 changes: 2 additions & 0 deletions reasonify-headless/reasonify/examples/dir.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
- source: |
import promplate
dir(promplate)
-
- source: end_of_turn()
4 changes: 4 additions & 0 deletions reasonify-headless/reasonify/examples/file.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
- source: |
reply(f"I can these files/dirs: {", ".join(map(str, paths))}")
end_of_turn()
- I just uploaded a PNG file, can you find it?

-
- source: |
list(Path("/workspace/mnt").rglob("*.png"))
result: no
-
- source: end_of_turn()
3 changes: 3 additions & 0 deletions reasonify-headless/reasonify/examples/search.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
open(url)
result:
return: true
-
- source: end_of_turn()
1 change: 1 addition & 0 deletions reasonify-headless/reasonify/examples/simple_ask.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
-
- source: |
reply("I am `Reasonify`, the first PythonAgent in the world. I interact with you through python code.")
end_of_turn()
23 changes: 21 additions & 2 deletions reasonify-headless/reasonify/templates/main.j2
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Here are all the functions you can use in your code:

Besides them, you can use any Python built-in library, such as `datetime`, `math`, `re`, etc.

Before using a third-party library, please install it first.
Remember to `pip_install` any third-party library you need before using it.

Mention that you can't use `subprocess` or `threading`. If you need to use some command line tool, use its programmatic API if available.

Expand All @@ -34,6 +34,25 @@ Remember: do not split your code into multiple blocks unless necessary. Only bre
<| system |>
Above are some examples, now let's interact with the real world.
As you've seen in the examples, your response should always be a list of string.
Never use code blocks, or return text directly!
Never use code blocks or return text directly!

Some hints on how to write best code:

1. You should call `end_of_turn()` only when you have finished processing the user's request and are ready to receive the next one.
2. If you are not sure about the user's input, ask for clarification by reply a question and then `end_of_turn`. Never make assumptions!
3. If your operation needs some personal information of the user, you can ask for it by calling `input` for text answer.
4. Always use `pathlib.Path` instead of `open` / `glob` for fs operations.
5. Prefer `from ... import ...` over `import ...`.
6. If user requests file processing, always make sure it is successfully processed before ending this turn.
7. Never assume a 3rd-party package is installed. You should `pip_install` it before importing it.
8. Some package doesn't work with pyodide. Don't worry. Try some alternatives.
9. Do not call `end_of_turn` when the result is uncertain (such as when success has not yet been confirmed or you have not yet seen the results from fetching a webpage).
10. Don't stop at the first failure! For example, if you can't find something on Wikipedia, try searching it with Google/duckduckgo or something else.
11. When searching the Internet, you can search several page at the same time, each with a few keywords, instead of searching a long query once.
12. When asked about some Real-time / factual things, especially when the user asks "What is ...", don't hesitate to search the internet for the latest information.
13. Carefully escaping characters! Because you are writing JSON, you need to escape the backslashes and double quotes in your strings.
14. Always format the output in a human-readable way before `reply` them to the user.
15. You can use `opencv-python` and `matplotlib` for image/video processing, and `numpy` and `pandas` for data processing.
16. When asked about programming-related questions, first consider searching GitHub / PyPI

{% chat -%}
7 changes: 6 additions & 1 deletion reasonify-headless/reasonify/utils/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ def stubfile():


@tool
def reply(_):
def reply(*_):
"""placeholder for examples to be runnable"""


@tool
def end_of_turn():
"""placeholder for examples to be runnable"""
2 changes: 1 addition & 1 deletion reasonify-headless/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.4"
__version__ = "0.2.0"

1 comment on commit c873c50

@vercel
Copy link

@vercel vercel bot commented on c873c50 Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

reasonify – ./

reasonify-git-main-promplate.vercel.app
reasonify-promplate.vercel.app
reasonify.vercel.app

Please sign in to comment.