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

Assistant: OpenAI and Anthropic #1476

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions module/core/collection_tools/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ pub mod llist;
pub mod vec;
/// [std::collections::VecDeque] macros
pub mod deque;

2 changes: 1 addition & 1 deletion module/core/format_tools/src/format/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,4 +584,4 @@ pub mod prelude
use super::*;
}

//
//
191 changes: 191 additions & 0 deletions module/core/format_tools/tests/inc/print_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#[ allow( unused_imports ) ]
use super::*;

use the_module::
{
Fields,
IteratorTrait,
AsTable,
Cells,
TableSize,
TableRows,
TableHeader,
Context,
WithRef,
MaybeAs,
};

use std::
{
collections::HashMap,
borrow::Cow,
};

/// Struct representing a test object with various fields.
#[ derive( Clone, Debug ) ]
pub struct TestObject
{
pub id : String,
pub created_at : i64,
pub file_ids : Vec< String >,
pub tools : Option< Vec< HashMap< String, String > > >,
}

impl Fields< &'static str, MaybeAs< '_, str, WithRef > >
for TestObject
{
type Value< 'v > = MaybeAs< 'v, str, WithRef >;

fn fields( &self ) -> impl IteratorTrait< Item = ( &'static str, MaybeAs< '_, str, WithRef > ) >
{
// use format_tools::ref_or_display_or_debug_multiline::field;
use format_tools::ref_or_display_or_debug::field;
let mut dst : Vec< ( &'static str, MaybeAs< '_, str, WithRef > ) > = Vec::new();

dst.push( field!( &self.id ) );
dst.push( field!( &self.created_at ) );
dst.push( field!( &self.file_ids ) );

if let Some( tools ) = &self.tools
{
dst.push( field!( tools ) );
}
else
{
dst.push( ( "tools", MaybeAs::none() ) );
}

dst.into_iter()
}
}

//

fn test_objects_gen() -> Vec< TestObject >
{

vec!
[
TestObject
{
id : "1".to_string(),
created_at : 1627845583,
file_ids : vec![ "file1".to_string(), "file2".to_string() ],
tools : None
},
TestObject
{
id : "2".to_string(),
created_at : 13,
file_ids : vec![ "file3".to_string(), "file4\nmore details".to_string() ],
tools : Some
(
vec!
[
{
let mut map = HashMap::new();
map.insert( "tool1".to_string(), "value1".to_string() );
map
},
{
let mut map = HashMap::new();
map.insert( "tool2".to_string(), "value2".to_string() );
map
}
]
),
},
]

}

//

#[ test ]
fn table_to_string()
// where
// for< 'a > AsTable< 'a, Vec< TestObject >, usize, TestObject, &'static str, String, &'static str > : TableFormatter< 'a >,
{
use the_module::TableToString;
let test_objects = test_objects_gen();

let cells = Cells::< &'static str, WithRef >::cells( &test_objects[ 0 ] );
assert_eq!( cells.len(), 4 );
let cells = Cells::< &'static str, WithRef >::cells( &test_objects[ 1 ] );
assert_eq!( cells.len(), 4 );
drop( cells );

let as_table : AsTable< '_, Vec< TestObject >, usize, TestObject, &str, WithRef > = AsTable::new( &test_objects );
let size = TableSize::mcells( &as_table );
assert_eq!( size, [ 2, 4 ] );
let rows = TableRows::rows( &as_table );
assert_eq!( rows.len(), 2 );
dbg!( rows.collect::< Vec< _ > >() );
let header = TableHeader::header( &as_table );
assert!( header.is_some() );
let header = header.unwrap();
assert_eq!( header.len(), 4 );
assert_eq!( header.clone().collect::< Vec< _ > >(), vec!
[
( "id", Cow::Owned( "id".to_string() ) ),
( "created_at", Cow::Owned( "created_at".to_string() ) ),
( "file_ids", Cow::Owned( "file_ids".to_string() ) ),
( "tools", Cow::Owned( "tools".to_string() ) )
]);
dbg!( header.collect::< Vec< _ > >() );

let mut output = String::new();
let mut context = Context::new( &mut output, Default::default() );
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( got.is_ok() );
println!( "{}", &output );

// with explicit arguments

let as_table : AsTable< '_, Vec< TestObject >, usize, TestObject, &str, WithRef > = AsTable::new( &test_objects );
let table_string = as_table.table_to_string();
assert!( table_string.contains( "id" ) );
assert!( table_string.contains( "created_at" ) );
assert!( table_string.contains( "file_ids" ) );
assert!( table_string.contains( "tools" ) );

// without explicit arguments

println!( "" );
let as_table = AsTable::new( &test_objects );
let table_string = as_table.table_to_string();
assert!( table_string.contains( "id" ) );
assert!( table_string.contains( "created_at" ) );
assert!( table_string.contains( "file_ids" ) );
assert!( table_string.contains( "tools" ) );
println!( "{table_string}" );

}

#[ test ]
fn custom_formatter()
{
// use the_module::TableToString;
let test_objects = test_objects_gen();

let mut output = String::new();
let mut formatter = the_module::Styles::default();
formatter.cell_separator = " | ".into();
formatter.row_prefix = "> ".into();
formatter.row_postfix = " <".into();

let as_table = AsTable::new( &test_objects );
let mut context = Context::new( &mut output, formatter );
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
assert!( got.is_ok() );
// let table_string = got.unwrap();

assert!( output.contains( "id" ) );
assert!( output.contains( "created_at" ) );
assert!( output.contains( "file_ids" ) );
assert!( output.contains( "tools" ) );
println!( "{output}" );

}

// xxx
99 changes: 99 additions & 0 deletions module/move/assistant/design/Commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Commands

## Legend

- `<...>` - argument.
- `<..?>` - optional argument.
- `<...=...>` - argument with default value.
- `!` - command requires a set of messages, however that set is hard to encode in CLI.

## OpenAI

### Users

```shell
assistant openai users list
assistant openai users modify <id> <role>
assistant openai users retrieve <id>
assistant openai users delete <id>
```

### Projects

```shell
assistant openai projects list <include_archived=false>
assistant openai projects create <name>
assistant openai projects retrieve <id>
assistant openai projects modify <id> <new_name>
assistant openai projects archive <id>
```

### Project users

```shell
assistant openai project-users list <project_id>
assistant openai project-users create <project_id> <user_id> <role>
assistant openai project-users retrieve <project_id> <user_id>
assistant openai project-users modify <project_id> <user_id> <role>
assistant openai project-users delete <project_id> <user_id>
```

### Project API keys

```shell
assistant openai project-api-keys list <project_id>
assistant openai project-api-keys retrieve <project_id> <key_id>
assistant openai project-api-keys delete <project_id> <key_id>
```

### Assistants

```shell
assistant openai assistants create <model> <name?> <description?> <instructions?>
assistant openai assistants list
assistant openai assistants retrieve <id>
assistant openai assistants modify <id> <model?> <name?> <description?> <instructions?>
assistant openai assistants delete <id>
```

### Threads

```shell
assistant openai threads create
assistant openai threads retrieve <id>
assistant openai threads delete <id>
```

### Messages

```shell
assistant openai messages create <thread_id> <role> <content>
assistant openai messages list <thread_id>
assistant openai messages retrieve <thread_id> <message_id>
assistant openai messages modify <thread_id> <message_id>
assistant openai messages delete <thread_id> <message_id>
```

### Chat

```shell
!assistant openai chat create-completion
```

### Runs

```shell
assistant openai runs create <thread_id> <assistant_id>
assistant openai runs list <thread_id>
assistant openai runs retrieve <thread_id> <run_id>
assistant openai runs cancel <thread_id> <run_id>
```

## Anthropic

### Messages

```shell
!assistant anthropic messages create
```

21 changes: 21 additions & 0 deletions module/move/assistant/design/Entities.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
graph TD
subgraph OpenAI
U[Users]
P[Projects]
PAK[Project API keys]
A[Assistants]
T[Threads]
M[Messages]
R[Runs]

P -- has (through Project Users) --> U
P -- has --> PAK

T -- has --> M

T -- has --> R

A -. are called in .-> R

R -. generate .-> M
end
Binary file added module/move/assistant/design/Entities.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading