-
Notifications
You must be signed in to change notification settings - Fork 129
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
Add CSV file reading functionality to the workspace file system #740
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,9 +235,9 @@ | |
|
||
## `retrieval` | ||
|
||
``` | ||
Usage: genaiscript retrieval|retreival [options] [command] | ||
|
||
Check notice on line 240 in docs/src/content/docs/reference/cli/commands.md GitHub Actions / build
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code block starting with 'Usage: genaiscript retrieval|retreival [options] [command]' should be fenced with triple backticks to maintain consistency with the document formatting.
|
||
RAG support | ||
RAG support | ||
|
||
Options: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }>' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect code fence, Typescript type annotation should not be in a string format.
|
||
const data = await workspace.readCSV<{ name: string; value: number }>( | ||
"data.csv" | ||
) | ||
``` | ||
|
||
### `writeText` | ||
|
||
Writes text to a file, relative to the workspace root. | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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" | ||
|
@@ -68,6 +69,14 @@ | |
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 | ||
Check failure on line 78 in packages/core/src/filesystem.ts GitHub Actions / build
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no error handling for the async function
|
||
}, | ||
cache: async (name: string) => { | ||
if (!name) throw new NotSupportedError("missing cache name") | ||
const res = JSONLineCache.byName<any, any>(name) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ | |
readText: (f) => runtimeHost.workspace.readText(f), | ||
readJSON: (f) => runtimeHost.workspace.readJSON(f), | ||
readXML: (f) => runtimeHost.workspace.readXML(f), | ||
readCSV: (f) => runtimeHost.workspace.readCSV(f), | ||
Check failure on line 67 in packages/core/src/promptcontext.ts GitHub Actions / build
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function
|
||
writeText: (f, c) => runtimeHost.workspace.writeText(f, c), | ||
cache: (n) => runtimeHost.workspace.cache(n), | ||
findFiles: async (pattern, options) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -579,6 +579,15 @@ | |
*/ | ||
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[]> | ||
Check failure on line 589 in packages/core/src/types/prompt_template.d.ts GitHub Actions / build
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
|
||
/** | ||
* Writes a file as text to the file system | ||
* @param path | ||
|
@@ -969,6 +978,11 @@ | |
|
||
type TokenEncoder = (text: string) => number[] | ||
|
||
interface CSVParseOptions { | ||
delimiter?: string | ||
headers?: string[] | ||
} | ||
|
||
interface Parsers { | ||
/** | ||
* Parses text as a JSON5 payload | ||
|
@@ -1034,7 +1048,7 @@ | |
*/ | ||
CSV( | ||
content: string | WorkspaceFile, | ||
options?: { delimiter?: string; headers?: string[] } | ||
options?: CSVParseOptions | ||
): object[] | undefined | ||
|
||
/** | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be a typo here.