Skip to content

[Assist] Replace embedding watcher#27953

Merged
jakule merged 15 commits intohugo/ai-embedding-watcherfrom
jakule/ai-embedding-nodes
Jun 20, 2023
Merged

[Assist] Replace embedding watcher#27953
jakule merged 15 commits intohugo/ai-embedding-watcherfrom
jakule/ai-embedding-nodes

Conversation

@jakule
Copy link
Copy Markdown
Contributor

@jakule jakule commented Jun 16, 2023

This PR changes the way how the embeddings are calculated. Instead of creating a watcher in Auth, we will process all nodes every hour and process embeddings if any embeddings are missing or any node has been updated.

TODO:

  • Add more tests.

@jakule jakule marked this pull request as ready for review June 16, 2023 20:20
@github-actions github-actions Bot requested review from probakowski and xacrimon June 16, 2023 20:21
@jakule jakule removed the request for review from probakowski June 16, 2023 20:21
Comment thread lib/ai/embeddings/embeddings_test.go Outdated
done := make(chan struct{})
go func() {
err := processor.Run(ctx, 100*time.Millisecond)
require.ErrorContains(t, err, "context canceled")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can't use require in a goroutine like this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I keep forgetting about this 🤦 I'll replace it with assert that work in this case.

Comment thread lib/ai/embeddings/embeddings_test.go Outdated

// Add some node servers.
nodes := make([]types.Server, 0, 5)
for i := 0; i < 5; i++ {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
for i := 0; i < 5; i++ {
for i := 0; i < cap(nodes); i++ {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replaced with a constant as I used 5 more times in this test

Comment thread lib/ai/embeddings/embeddings_test.go Outdated

require.Eventually(t, func() bool {
items, err := stream.Collect(embeddings.GetEmbeddings(ctx, types.KindNode))
require.NoError(t, err)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Similarly, eventually runs the condition in a background goroutine - so we shouldn't use require here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

eventually runs the condition in a background goroutine

Wasn't aware. Fixed.

@jakule jakule requested a review from zmb3 June 20, 2023 14:49
Comment thread lib/ai/embeddings/embeddings.go
Comment thread lib/ai/embeddings/embeddings.go Outdated
Comment thread lib/ai/embeddings/embeddings.go Outdated
Comment thread lib/ai/embeddings/embeddings.go Outdated
Comment thread lib/ai/embeddings/embeddings.go Outdated
Comment thread lib/utils/stream/zip.go Outdated
Comment thread lib/utils/stream/zip.go Outdated
Comment thread lib/utils/stream/zip.go Outdated
Comment thread lib/utils/stream/zip.go Outdated
Comment thread lib/utils/stream/zip.go Outdated
@jakule jakule requested a review from rosstimothy June 20, 2023 18:38
Comment thread lib/ai/embeddings.go
}

// BatchReducer is a helper that processes data in batches.
type BatchReducer[T, V any] struct {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please indicate that this type and it's receiver methods are not thread safe.

Comment thread lib/ai/embeddings.go Outdated
Comment thread lib/ai/embeddings.go
Comment on lines +41 to +71
type Embeddings interface {
// GetEmbedding looks up a single embedding by its name in the backend.
GetEmbedding(ctx context.Context, kind, resourceID string) (*Embedding, error)
// GetEmbeddings returns all embeddings for a given kind.
GetEmbeddings(ctx context.Context, kind string) stream.Stream[*Embedding]
// UpsertEmbedding creates or update a single ai.Embedding in the backend.
UpsertEmbedding(ctx context.Context, embedding *Embedding) (*Embedding, error)
}

// MarshalEmbedding marshals the ai.Embedding resource to binary ProtoBuf.
func MarshalEmbedding(embedding *Embedding) ([]byte, error) {
data, err := proto.Marshal((*embeddingpb.Embedding)(embedding))
if err != nil {
return nil, trace.Wrap(err)
}
return data, nil
}

// UnmarshalEmbedding unmarshals binary ProtoBuf into an ai.Embedding resource.
func UnmarshalEmbedding(bytes []byte) (*Embedding, error) {
if len(bytes) == 0 {
return nil, trace.BadParameter("missing embedding data")
}
var embedding embeddingpb.Embedding
err := proto.Unmarshal(bytes, &embedding)
if err != nil {
return nil, trace.Wrap(err)
}

return (*Embedding)(&embedding), nil
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Most other backend services and marshaling functions live in lib/services. Is there any reason that we are deviating from that here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

lib/services is already big and this code is used in a few places in the code introduced in this PR. In order to not introduce a cyclic dependency and not keep everything in lib/services I moved the marshal/unmarshal functions to outside of it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd prefer if we were consistent and kept things within lib/services. I think that the import cycle is coming from services.NodesStreamGetter but IMO that should just be defined within Presence and not the smaller interface. Consumers should define their own subset of Presence which would put the interface closer to the consumer and follow the guidance here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I removed the cyclic dependency and moved the interface to lib/services.

@jakule jakule requested a review from rosstimothy June 20, 2023 19:49
@jakule jakule merged commit 3895105 into hugo/ai-embedding-watcher Jun 20, 2023
@jakule jakule deleted the jakule/ai-embedding-nodes branch June 20, 2023 23:51
github-merge-queue Bot pushed a commit that referenced this pull request Jun 21, 2023
* ai: add embeddings basic support

- add Embeddings service and its local implementation
- add Embedding type and proto message
- add nodeEmbeddingCollector tracking nodes
- add NodeEmbeddingWatcher watching for events adn sending them to the
  collector
- add the Embedder interface and its openai implementation

* ai: adapt embeddings to the vector index

* fixup! ai: adapt embeddings to the vector index

* fixup! fixup! ai: adapt embeddings to the vector index

* Update lib/service/service.go

Co-authored-by: Jakub Nyckowski <jakub.nyckowski@goteleport.com>

* address feedback pt.1

* address feedback pt.2: store protobuf message in backend

* address feedback pt.3: have GetEmbeddings return a stream

* Update lib/services/embeddings.go

Co-authored-by: rosstimothy <39066650+rosstimothy@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Edoardo Spadolini <edoardo.spadolini@goteleport.com>

* address feedback pt.4: extract embedding logic out of Embeddings service

* fixup! address feedback pt.4: extract embedding logic out of Embeddings service

* address feedback pt.5: simpler error handling when embedding fails

* fix tests pt.1

* fix tests pt.2

* fix tests pt.3

* [Assist] Replace embedding watcher (#27953)

Change the way how the embeddings are calculated. Instead of creating a watcher in Auth, we will process all nodes every hour and process embeddings if any embeddings are missing or any node has been updated.

---------

Co-authored-by: Jakub Nyckowski <jakub.nyckowski@goteleport.com>
Co-authored-by: rosstimothy <39066650+rosstimothy@users.noreply.github.com>
Co-authored-by: Edoardo Spadolini <edoardo.spadolini@goteleport.com>
jakule added a commit that referenced this pull request Jun 29, 2023
* ai: add embeddings basic support

- add Embeddings service and its local implementation
- add Embedding type and proto message
- add nodeEmbeddingCollector tracking nodes
- add NodeEmbeddingWatcher watching for events adn sending them to the
  collector
- add the Embedder interface and its openai implementation

* ai: adapt embeddings to the vector index

* fixup! ai: adapt embeddings to the vector index

* fixup! fixup! ai: adapt embeddings to the vector index

* Update lib/service/service.go

Co-authored-by: Jakub Nyckowski <jakub.nyckowski@goteleport.com>

* address feedback pt.1

* address feedback pt.2: store protobuf message in backend

* address feedback pt.3: have GetEmbeddings return a stream

* Update lib/services/embeddings.go

Co-authored-by: rosstimothy <39066650+rosstimothy@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Edoardo Spadolini <edoardo.spadolini@goteleport.com>

* address feedback pt.4: extract embedding logic out of Embeddings service

* fixup! address feedback pt.4: extract embedding logic out of Embeddings service

* address feedback pt.5: simpler error handling when embedding fails

* fix tests pt.1

* fix tests pt.2

* fix tests pt.3

* [Assist] Replace embedding watcher (#27953)

Change the way how the embeddings are calculated. Instead of creating a watcher in Auth, we will process all nodes every hour and process embeddings if any embeddings are missing or any node has been updated.

---------

Co-authored-by: Jakub Nyckowski <jakub.nyckowski@goteleport.com>
Co-authored-by: rosstimothy <39066650+rosstimothy@users.noreply.github.com>
Co-authored-by: Edoardo Spadolini <edoardo.spadolini@goteleport.com>
github-merge-queue Bot pushed a commit that referenced this pull request Jul 3, 2023
* [Assist] Scaffold the chat-loop onto a multi-step thinking model (#27075)

* agent scaffold conversion

* command input validation

* rename Agent.Think and replace debug logs with trace logs

* doc

* action docs

* godocs

* clarify

* remove unused code

* remove tests which relied on the old non-agent model interaction with the llm

* fix broken e

* Add node name to the Assist execution result (#27635)

* Add node name to the Assist execution result

Currently, only node ID is returned on the command execution result in Assist. For better UX we want to display Node name which id more human friendly rather than a node ID which is a UUID. Adding the value to returned payload sounds cheaper than calling an API to get node names.

* Add test

* Extract commandExecResult struct

* Fix test after rebase

* Fix command execution test flakiness (#27704)

Fix
```
--- FAIL: TestExecuteCommand (1.46s)
    testing.go:1206: TempDir RemoveAll cleanup: unlinkat /tmp/TestExecuteCommand3553793052/002/log/upload/streaming/default: directory not empty
FAIL
```
error

* [Assist] Fix panic when writing to one WS from multiple threads (#27828)

* [Assist] Fix panic when writing to one WS from multiple threads

 Fixes https://github.com/gravitational/teleport.e/issues/1650

* Remove mutex on SetReadDeadline

* Move SetPongHandler

* Fix typos

* Fix command output showing when running on multiple nodes (#27936)

* ai: Add a node embedding watcher (#27204)

* ai: add embeddings basic support

- add Embeddings service and its local implementation
- add Embedding type and proto message
- add nodeEmbeddingCollector tracking nodes
- add NodeEmbeddingWatcher watching for events adn sending them to the
  collector
- add the Embedder interface and its openai implementation

* ai: adapt embeddings to the vector index

* fixup! ai: adapt embeddings to the vector index

* fixup! fixup! ai: adapt embeddings to the vector index

* Update lib/service/service.go

Co-authored-by: Jakub Nyckowski <jakub.nyckowski@goteleport.com>

* address feedback pt.1

* address feedback pt.2: store protobuf message in backend

* address feedback pt.3: have GetEmbeddings return a stream

* Update lib/services/embeddings.go

Co-authored-by: rosstimothy <39066650+rosstimothy@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Edoardo Spadolini <edoardo.spadolini@goteleport.com>

* address feedback pt.4: extract embedding logic out of Embeddings service

* fixup! address feedback pt.4: extract embedding logic out of Embeddings service

* address feedback pt.5: simpler error handling when embedding fails

* fix tests pt.1

* fix tests pt.2

* fix tests pt.3

* [Assist] Replace embedding watcher (#27953)

Change the way how the embeddings are calculated. Instead of creating a watcher in Auth, we will process all nodes every hour and process embeddings if any embeddings are missing or any node has been updated.

---------

Co-authored-by: Jakub Nyckowski <jakub.nyckowski@goteleport.com>
Co-authored-by: rosstimothy <39066650+rosstimothy@users.noreply.github.com>
Co-authored-by: Edoardo Spadolini <edoardo.spadolini@goteleport.com>

* Restore `lib/ai` tests (#28077)

* Restore `lib/ai` tests

The tests were removed as a part of #27075.

This PR updates the tests to use the new logic.

* Fix tests

* Restore lib/web tests

* GCI

* Move test handler to a common place

* Fix used token test

* Add comment

* Remove duplicate imports (#27886)

* [Assist] Remove the empty assist message (#28125)

* [Assist] Remove the empty assist message

Assist shows an empty message at the beginning of each conversation when reading it from DB. This PR fixes that behavior and adds a test to prevent this from happening in the future.

* Address code review comments

* Address code review comments

* Skip embedding processor on Cloud Non-Team plan (#28197)

* ai: compute opportinistic summary of command execution (#28033)

* ai: compute opportinistic summary of command execution

* ai: add streaming summary back after rebase on new front-end

* Lint and fix tests pt.1

* reference nodes by name and add tests

* Lint, fix tests and address feedback

* Attempt to tame the stream close monster

* fixup! Attempt to tame the stream close monster

* [Assist] Do not close the WS after command execution (#28246)

* Revert "fixup! Attempt to tame the stream close monster"

This reverts commit 8537aa2.

* Revert "Attempt to tame the stream close monster"

This reverts commit e0c861d.

* Do not close the WS after command execution

* Fix tests and lint

* fixup! Fix tests and lint

* undo put web test command into constant

---------

Co-authored-by: Jakub Nyckowski <jakub.nyckowski@goteleport.com>

* [Assist] Include embeddings in the prompt (#28116)

* [Assist] Include embeddings in the prompt

* Add comments
GCI
Minor fixes

* Move stuff

* Fix tests

* Fix tests

* Fixes after rebase
Apply code review suggestions.

* Address review comments

* After rebase fix

* Improve error handling and embedding prompts; fix typos (#28403)

* "Improve error handling and embedding prompts; fix typos"

This commit encompasses several changes. First, an error handling routine has been added in AssistContext.tsx to properly close a WebSocket connection and finish all results. The intent is to ensure that execution fails gracefully when a session doesn't end normally. In tool.go, user instructions have been made more explicit to ensure users check access to nodes before generating any commands. It warns them that not checking access will cause error. Also, some minor typos were corrected in agent.go and messages.go for better readability.

* "Refactor 'hosts' to 'nodes' in AI Tool Descriptions"

This commit refactors the language from 'host' terminology to 'node' terminology in the AI tool's generated responses as the LLM seems to be confused when generating queries with embeddings.

* Update expected test values in chat_test.go

The expected values in three different tests in chat_test.go have been updated. This change was required because the underlying algorithm has been adjusted and these modifications will keep the tests aligned with the current algorithm's behavior.

* Add missing imports

* Introduce user preferences (#28291)

* Add user preferences feature

* Add missing license header

* Fix the order of arguments to require.Equal

* Update lib/web/userpreferences.go

Co-authored-by: Michelle Bergquist <11967646+michellescripts@users.noreply.github.com>

* Add a `GetUserPreferencesResponse` message

* Remove unused logger

* Use .Put instead of .Create/.Update

* Add missing godoc

* trace.Wrap the happy path

---------

Co-authored-by: Michelle Bergquist <11967646+michellescripts@users.noreply.github.com>

* Shut down embedding processor on graceful exit (#28356)

* Refactor websocket termination and stream handling (#28452)

* Refactor websocket termination and stream handling

Refactored websocket stream shutdown and error handling. Replaced `Close()` with `SendCloseMessage()` for better control over the websocket connection termination process. Added checks for the validity of channels to prevent reading from closed channels.
The commit also includes minor typo fixes.

* Remove unused completedC

* Remove unnecessary select blocks in terminal.go

The select blocks used in terminal.go for reading data from channels were unnecessary as we were just pulling from a single channel. Removed the select block and directly attempted to read from the channel. These changes increase code readability and integrity by removing unnecessary select blocks. In the command_test.go, an explanatory comment was added for clarity.

* Remove commented code

* Replace trace.NewAggregate with trace.Wrap as aggregation is not needed.

* Add the UI for Assist's settings (#28413)

* Add the UI for Assist's settings

* Add typing

* Fix test by wrapping render in LayoutContextProvider

* Run prettier

* Assist: fix summary logic (#28487)

* Update command.go

* simplify export signature

* assist: add classification code (#28221)

* [Assist] Provide interactive updates during agent execution (#27893)

* send progress update messages during agent thoughts

* handle new output format

* define json tags for serialized fields

* use streaming api

* fan streaming from model loop

* fix streaming

* stream progress updates

* Update lib/assist/assist.go

Co-authored-by: Jakub Nyckowski <jakub.nyckowski@goteleport.com>

* remove useless mute

* nits

* Update lib/ai/model/agent.go

Co-authored-by: Jakub Nyckowski <jakub.nyckowski@goteleport.com>

* fix merge

* fix misc

* more misc fixes

* what

* what2

* weird eof errors?

* Fix tests UI integration

* Fix other tests

* Linter fixes

* Comment out token counting for assist streams to avoid race condition.

* Fix more tests

---------

Co-authored-by: Jakub Nyckowski <jakub.nyckowski@goteleport.com>

* Remove console.log in AssistContext (#28607)

---------

Co-authored-by: Joel <jwejdenstal@goteleport.com>
Co-authored-by: Ryan Clark <ryan.clark@goteleport.com>
Co-authored-by: Hugo Shaka <hugo.hervieux@goteleport.com>
Co-authored-by: rosstimothy <39066650+rosstimothy@users.noreply.github.com>
Co-authored-by: Edoardo Spadolini <edoardo.spadolini@goteleport.com>
Co-authored-by: Zac Bergquist <zac.bergquist@goteleport.com>
Co-authored-by: Justinas Stankevičius <justinas@users.noreply.github.com>
Co-authored-by: Michelle Bergquist <11967646+michellescripts@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants