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

Generic interface #106

Closed
wants to merge 35 commits into from
Closed

Generic interface #106

wants to merge 35 commits into from

Conversation

waydegg
Copy link
Contributor

@waydegg waydegg commented Sep 23, 2023

Summary

This PR covers:

  • updating all blocking code to be async
  • creating a standard message format between the core library and client libraries
  • moving all terminal-specific logic and dependencies under mentat/terminal

Things that will be covered in a later PR:

  • A JSON RPC server with an LSP-compantible API that any editors with language server support can connect to
  • A SessionManager to coordinate multiple, concurrent chats

To-do

  • merge with main and resolve merge conflicts
  • validate cprint colors received from core
  • check/test for race conditions b/w core and terminal client (specifically w/ inputs and interrupts)
  • fix test cases
  • create existing test cases
    • core library
    • terminal client
  • fix misc typing errors

@granawkins granawkins mentioned this pull request Sep 24, 2023
Copy link
Member

@granawkins granawkins left a comment

Choose a reason for hiding this comment

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

Generally it looks great - seems to check all the boxes we identified with the concepts and what not. Nicely done 👏


logger = logging.getLogger()

_SESSION_STREAM: ContextVar[SessionStream] = ContextVar("mentat:session_stream")
Copy link
Member

Choose a reason for hiding this comment

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

Like I said on the call - I really like this approach to 'instancing'. I think it will make sense to do the same thing with CodeContext eventually.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah we might want to do this with the entire session instance at some point

@dataclass
class StreamMessage:
id: UUID
channel: str
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this could be 'default', 'input_request', a UUID, 'input_request:{UUID}' or 'interrupt'. It'd be nice to have these as an enum or something, but I'm not sure the best way to handle the uuids..

# response = await stream.recv(f"default:{message.id}")

message = await stream.send("", channel="input_request")
response = await stream.recv(f"input_request:{message.id}")
Copy link
Member

@granawkins granawkins Sep 27, 2023

Choose a reason for hiding this comment

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

I could imagine async tasks in the background could use this approach to. e.g. there could be an EmbeddingProvider listening on "embed_request".

Should there be an optional expire somewhere, maybe in recv, to handle network failures and the like?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally. Adding more keys/values to the StreamMessage in the future won't break anything with how it's setup atm

channel: str
source: StreamMessageSource
data: Any
extra: Dict[str, Any] | None
Copy link
Member

Choose a reason for hiding this comment

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

What if instead of "color" we used sth like debug level - "info", "warning", "error" - which terminal.output can interpret to colors.

@biobootloader
Copy link
Member

biobootloader commented Sep 27, 2023

I'm confused about why Github is showing a bunch of changes that have already been merged as part of the diff for this PR. Like my change here for PyPI going from mentat-ai to mentat is showing up in this PR:

Maybe it's due to the conflicts, it's not able to show a diff against main for some reason?

Edit: Is it from merging with other branches prior to them being merged to main? So the changes come from different commits (since we squash when merging with main)? Though I still don't understand

image

Copy link
Member

@PCSwingle PCSwingle left a comment

Choose a reason for hiding this comment

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

Really love this! I'll take a closer look once it gets caught up to main, but looks good to me so far! Left a few questions on a couple things I wasn't sure about.



class Event:
def __init__(self, channel: str, message: typing.Any) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

nit: Why is this typing.Any instead of Any?



def get_previous_lines(code_change, num=2):
async def print_previous_lines(code_change: CodeChange, num: int = 2):
Copy link
Member

Choose a reason for hiding this comment

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

Why did we change these to printing functions instead of just returning the string?

logger.debug("Task has stopped")

self._stop_task = asyncio.create_task(run_stop())
self._stop_task.add_done_callback(cleanup_stop)
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason we need to have a done_callback instead of just doing this at the end of cleanup_stop?

@jakethekoenig
Copy link
Member

I added ipdb to requirements.txt

@jakethekoenig
Copy link
Member

There was a git merge conflict token in the logging_config I removed. It's now mostly working for me. I notice a few things though:

  1. ctrl-c while the model is streaming closes mentat completely instead of returning to prompt.
  2. Colors aren't working for me in streamed output removal. e.g. see the following conversation with coloring symbols instead of colors:
(.venv) Development/mentat % mentat README.md
Files included in context:
mentat
└── README.md

Including CodeMap (filepaths only)
File and prompt token count: 5961 / 8192
Type 'q' or use Ctrl-C to quit at any time.
What can I do for you?
>>> Please delete the fifth through seventh lines of the readme

Including CodeMap (filepaths only)
Total token count: 5971
Streaming(.venv) Development/mentat % mentat README.md
Files included in context:
mentat
└── README.md

Including CodeMap (filepaths only)
File and prompt token count: 5961 / 8192
Type 'q' or use Ctrl-C to quit at any time.
What can I do for you?
>>> Please delete the fifth through seventh lines of the readme

Including CodeMap (filepaths only)
Total token count: 5971
Streaming.e control-c to interrupt the model atu any point
I will delete lines 5 through 7 in the README.md file.

Steps:
1. Delete lines 5-7 in README.md

?[94m
README.md?[0m
============================================================
2:  [?[94m![Discord Follow?[39;49;00m](?[96mhttps://dcbadge.vercel.app/api/server/XbPdxAMJte?style=flat?[39;49;00m)](https://discord.gg/zbvd9qx9Pb)?[90m?[39;49;00m
3:  [?[94m![Stable Version?[39;49;00m](?[96mhttps://img.shields.io/pypi/v/mentat?color=blue?[39;49;00m)](https://pypi.org/project/mentat/)?[90m?[39;49;00m
4:  [?[94m![License?[39;49;00m](?[96mhttps://img.shields.io/pypi/l/mentat.svg?[39;49;00m)](https://github.com/AbanteAI/mentat/blob/main/LICENSE)
?[31m-
-   # 🧙‍♂️ Mentat ⚡
-   ?[0m
8:  > _It is by will alone I set my mind in motion_?[90m?[39;49;00m
9:  >
============================================================

Colors do seem to be working for inserts and system messages.
3. When mentat encounters an error e.g. the model doesn't correctly respond with line numbers then mentat starts streaming all the logs to stdout. Not sure if that's the intended behavior.

await self._main()
await self._shutdown()
# NOTE: if an exception is caught here, the main process will likely still run
# due to background ascynio Tasks that are still running
Copy link
Member

Choose a reason for hiding this comment

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

nit: asyncio

@granawkins
Copy link
Member

It looks like we lost the call to setup_api_key(), so I added it back in.

  1. ctrl-c while the model is streaming closes mentat completely instead of returning to prompt.
  2. Colors aren't working for me in streamed output removal. e.g. see the following conversation with coloring symbols instead of colors:
  3. When mentat encounters an error e.g. the model doesn't correctly respond with line numbers then mentat starts streaming all the logs to stdout. Not sure if that's the intended behavior.

I'm setting the same errors as @jakethekoenig. I also got some logging to the terminal when running a diff:

granthawkins@Grants-MacBook-Air mentat % mentat --diff HEAD~1                                     
Diff annotations:
 ─•─ HEAD~1: a3063419: Fix logging config git conflict token | 2 files | 1 lines

DEBUG:root:Skipping /Users/granthawkins/mentat/.gitignore. Reason: lexer not found
Files included in context:
mentat
├── * .gitignore
└── mentat
    └── session.py
INFO:root:StreamMessage(id=UUID('71ba8857-3bb6-466f-9411-b3dce0a2d980'), channel='default', source=<StreamMessageSource.SERVER: 'server'>, data='\nIncluding CodeMap (filepaths only)', extra={'color': 'green'}, created_at=datetime.datetime(2023, 9, 28, 23, 31, 2, 618374))
INFO:root:StreamMessage(id=UUID('9651a3bf-e483-48da-8ca7-23c6a84470be'), channel='default', source=<StreamMessageSource.SERVER: 'server'>, data='File and prompt token count: 5979 / 8192', extra={'color': 'cyan'}, created_at=datetime.datetime(2023, 9, 28, 23, 31, 2, 621870))
INFO:root:StreamMessage(id=UUID('470117d2-e6c3-43e5-a705-672a525a62b0'), channel='default', source=<StreamMessageSource.SERVER: 'server'>, data="Type 'q' or use Ctrl-C to quit at any time.", extra={'color': 'cyan'}, created_at=datetime.datetime(2023, 9, 28, 23, 31, 2, 621913))
INFO:root:StreamMessage(id=UUID('d3598fd7-8925-4043-a805-c6fcc927b09d'), channel='default', source=<StreamMessageSource.SERVER: 'server'>, data='What can I do for you?', extra={'color': 'light_blue'}, created_at=datetime.datetime(2023, 9, 28, 23, 31, 2, 621932))
INFO:root:StreamMessage(id=UUID('a3f188bb-aabb-437b-96e2-7e58550f218f'), channel='input_request', source=<StreamMessageSource.SERVER: 'server'>, data='', extra={}, created_at=datetime.datetime(2023, 9, 28, 23, 31, 2, 621953))

Including CodeMap (filepaths only)
File and prompt token count: 5979 / 8192
Type 'q' or use Ctrl-C to quit at any time.
What can I do for you?

@waydegg
Copy link
Contributor Author

waydegg commented Sep 29, 2023

Thanks for all of the comments everyone! I'm going to close and re-open this PR in an effort to get Github to show the proper git history (this might not be a github thing but we'll quickly find out lol).

@waydegg waydegg closed this Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants