Skip to content

Commit

Permalink
Add CSV file reading functionality to the workspace file system (#740)
Browse files Browse the repository at this point in the history
* Fix typo in CLI usage and add readCSV method to WorkspaceFileSystem interface

* Add CSV file reading functionality to the workspace file system

* Enhance readCSV function with generic type support and optional parsing options
  • Loading branch information
pelikhan authored Sep 30, 2024
1 parent 3081ffa commit 7296b07
Show file tree
Hide file tree
Showing 23 changed files with 304 additions and 25 deletions.
16 changes: 15 additions & 1 deletion docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/src/content/docs/reference/cli/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ Options:

## `retrieval`

```
Usage: genaiscript retrieval|retreival [options] [command]
RAG support
RAG support
Options:
Expand Down
16 changes: 16 additions & 0 deletions docs/src/content/docs/reference/scripts/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ Reads the content of a file as XML.
const data = await workspace.readXML("data.xml")
```

### `readCSV`

Reads the content of a file as CSV.

```ts
const data = await workspace.readCSV("data.csv")
```

In Typescript, you can type the output.

```ts '<{ name: string; value: number }>'
const data = await workspace.readCSV<{ name: string; value: number }>(
"data.csv"
)
```

### `writeText`

Writes text to a file, relative to the workspace root.
Expand Down
16 changes: 15 additions & 1 deletion genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion packages/auto/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions packages/core/src/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { JSONLineCache } from "./cache"
import { DOT_ENV_REGEX } from "./constants"
import { CSVParse } from "./csv"
import { NotSupportedError, errorMessage } from "./error"
import { resolveFileContent } from "./file"
import { readText, writeText } from "./fs"
Expand Down Expand Up @@ -68,6 +69,14 @@ export function createFileSystem(): Omit<WorkspaceFileSystem, "grep"> {
const res = XMLParse(file.content)
return res
},
readCSV: async <T extends object>(
f: string | Awaitable<WorkspaceFile>,
options?: CSVParseOptions
): Promise<T[]> => {
const file = await fs.readText(f)
const res = CSVParse(file.content, options) as T[]
return res
},
cache: async (name: string) => {
if (!name) throw new NotSupportedError("missing cache name")
const res = JSONLineCache.byName<any, any>(name)
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/src/promptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export async function createPromptContext(
readText: (f) => runtimeHost.workspace.readText(f),
readJSON: (f) => runtimeHost.workspace.readJSON(f),
readXML: (f) => runtimeHost.workspace.readXML(f),
readCSV: (f) => runtimeHost.workspace.readCSV(f),
writeText: (f, c) => runtimeHost.workspace.writeText(f, c),
cache: (n) => runtimeHost.workspace.cache(n),
findFiles: async (pattern, options) => {
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,15 @@ interface WorkspaceFileSystem {
*/
readXML(path: string | Awaitable<WorkspaceFile>): Promise<any>

/**
* Reads the content of a CSV file.
* @param path
*/
readCSV<T extends object>(
path: string | Awaitable<WorkspaceFile>,
options?: CSVParseOptions
): Promise<T[]>

/**
* Writes a file as text to the file system
* @param path
Expand Down Expand Up @@ -969,6 +978,11 @@ interface ParseZipOptions {

type TokenEncoder = (text: string) => number[]

interface CSVParseOptions {
delimiter?: string
headers?: string[]
}

interface Parsers {
/**
* Parses text as a JSON5 payload
Expand Down Expand Up @@ -1034,7 +1048,7 @@ interface Parsers {
*/
CSV(
content: string | WorkspaceFile,
options?: { delimiter?: string; headers?: string[] }
options?: CSVParseOptions
): object[] | undefined

/**
Expand Down
16 changes: 15 additions & 1 deletion packages/sample/genaisrc/blog/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion packages/sample/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion packages/sample/genaisrc/node/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion packages/sample/genaisrc/python/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7296b07

Please sign in to comment.