psycode

command module
v0.0.0-...-fe44f61 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

PsyCode

This project provides a command-line application that uses a large language model (LLM) for text analysis, supporting content coding in psychology research. psycode uses the Anthropic Claude API, and it's similar to gpt, which uses the OpenAI API.

PsyCode can be used for scoring and/or categorizing textual responses provided by study participants. You create a prompt template file with substitution variables like {{fieldName}} and you specify a CSV file containing the study data. PsyCode will work through each record in your dataset, sending prompts to the LLM, recording the responses, and extracting scores and/or categories as specified. The results are stored in an output CSV file that matches the input CSV file, except with a few new columns containing the content coding results.

PsyCode supports two different approaches to content coding. In the first case, you use the text command to generate a block of text (the model's response), from which PsyCode can extract the code(s) you're looking for. In the second case, you use the json command to generate one or more structured outputs (the codes you're looking for). This is the preferred method for content coding.

Developer Workstation Setup

make tools
  • Configure the following environment variables for your Claude API account. For simplicity, you could put them in your .bashrc, .zshrc, or equivalent. Substitute the placeholders below with your own values, of course.
export ANTHROPIC_API_KEY="sk-ant-usr-xxxxxxxxxxxxxxxxxxxxxxxxxxx-..."
export ANTHROPIC_WORKSPACE="wrkspc_xxxxxxxxxxxxxxxxxxxxxxxx"
  • Alternatively, you can create a file named .env and place the above environment variables there. Example contents:
ANTHROPIC_API_KEY="sk-ant-usr-xxxxxxxxxxxxxxxxxxxxxxxxxxx-..."
ANTHROPIC_WORKSPACE="wrkspc_xxxxxxxxxxxxxxxxxxxxxxxx"
  • Build the application for running in your local development environment:
make build

Install the Command-line Application

If you don't intend to modify the application, you can install it on your workstation by simply downloading the appropriate executable application and running it locally from the command-line.

If your workstation is set up for Go language development, you can install the latest version from source using the following command:

go install github.com/voxtechnica/psycode@latest

Run the Command-line Application

To run the command-line application, be sure to either set the environment variables identified above or create a .env file for your Claude API account. To verify that the application is configured properly, you can run the psycode about command. It will show your Workspace ID and API key.

You can explore the available commands, subcommands, and optional flags using the -h or --help flags with the executable application. For example:

./psycode -h
./psycode model -h
./psycode model list -h
./psycode model read -h
./psycode json -h
./psycode json prompt -h
./psycode json single -h
./psycode json batch -h
./psycode text -h
./psycode text prompt -h
./psycode text single -h
./psycode text batch -h

Listing the models is a convenient way to verify that you can access the Claude API as expected.

Also, you can explore the CLI documentation.

Working with CSV Files

Some of the commands (e.g. text single and text batch) use CSV files for data inputs and outputs. The first line of these files is expected to be column labels, and they need to be unique. If the column label is missing, it gets labeled as "columnX" where X is the column number. The subsequent lines of the CSV file are individual rows of data.

When you're using the text single and text batch commands, you can refer to different fields in your dataset by using the column labels. They're case-sensitive, and ideally, have no spaces. Good examples are id, question, and answer. You can include extra columns in your CSV files, and they're either ignored or reproduced faithfully in your output file.

If you want to refer to a specific field in a specific row of your dataset, you'll be supplying two arguments: the name and value of an "id" field, and the name of the field that contains the value you're referring to. The "id" field will be a name=value pair, such as pid=61324.

Using the text Command

The text command has five subcommands: prompt, single, parallel, batch, and results. The first two commands will typically be used when engineering an effective prompt for use with a batch of data. The prompt command works with just two text files: a prompt file and an optional system file. These files are plain UTF-8 text. The system file can be used to inform the model of its identity and the role it is expected to play. The prompt file then provides instructions for the model. See Anthropic's prompting best practices for more information. Note that the system file gets cached for repeated use, so this is an excellent place to document your content coding scheme. This reduces your input tokens and their associated cost. The part that changes with each request goes in your prompt file.

Note that for the single, parallel, and batch commands, the prompt file can contain template substitution variables. {{question}} is used for an optional question coming from a CSV file containing a column of question IDs and a column of questions. You'll be specifying these column names as command line flags. Whereas the question variable is optional (because the question could simply be stated as explicit text in your prompt), you'll definitely want to include one or more {{fieldName}} variables in your prompt file. Substitute the actual column names in your answers CSV file for "fieldName", and note the lack of spaces and case-sensitivity of the substitution variables.

So, the prompt command is used for simple prompts, and when you're ready to start experimenting with question(s) and answers in your CSV dataset, you can use the single command to test your prompt with different values. If you want to finesse the prompt with a specific answer, you can use the --answer-id flag with the single command to select the specified answer (e.g. -A pid=61324).

Once you're happy with the results you're seeing, you can use the parallel or batch commands to process the entire dataset. The parallel command works through your records in real time, sending multiple client-side requests in parallel. The batch command generates an asynchronous batch job, wherein Anthropic manages the concurrency. Using batch is recommended, as it costs about half as much as the real-time requests. Also, in testing, it appears to complete very quickly.

The text commands can also parse "scores" (numbers) from the model's response text. The --score-select flag indicates whether you'd like the first number found in the text, the last number, all the numbers, or none of the numbers (i.e. don't bother parsing scores). You'll probably just want the last number. And, of course, your prompt file will be providing instructions to the LLM for scoring the results.

The text commands can also be used for categorizing text. You'll provide the expected categorization terms using the category-terms flag and a comma-separated list of expected terms. Example: -T Good,Bad,Neutral. Use the category-select flag to parse out the first, last, or all of the expected terms in the response. If you select first or last, then you'll probably want to specify the category-field name for storing results in your output CSV file. If you select all category terms present in the response, then the terms become column names in your output CSV file, and if the term is present, the column will contain a 1 value. If the term is not present in the response for that record, then the column will be blank.

When you use the parallel or batch commands, the output CSV file will contain all the data provided in your input answer file, along with a few new columns. The textID column will contain a unique ID used with the model, and the generation column will contain the exact text of the model's response. If you're also parsing scores or category terms from the response, those column(s) will be included too. The default name for a score column is score or scoreX where X is a number indicating the position of the score in the list, if multiple scores are being parsed from the response text. Note that you can change the name of the score column using the --score-field flag. This can be useful if you're storing the results from multiple runs in the same CSV file. You can just give each run a different score field name, reusing the output file as an input file. The new score columns will be appended with each run.

One more tip on using questions and answers: if you have multiple questions, and your answer file includes answers to different questions, you can include a question ID column in your answers file. Then, when processing each answer, the system will look up the appropriate question to use for that answer in the questions CSV file. The question ID column name (e.g. qid) must be the same in both the questions file and the answers file. The --question-id flag controls this behavior. If you specify just a question ID field name, then it will be assumed that the field exists in both files. If you specify a name=value pair, then just that specific question will be used for the entire set of answers. Also, note that questions are optional. If all you have to process are "answers", then you can ignore the question bits.

Using the json Command

The json command is functionally similar to the text command, but much simpler in the invocation. It has far fewer command-line flags since you don't need to specify how to extract scores or categories from a generated block of text. Instead, the model simply follows your instructions provided in system and prompt files, generating an array of one or more structured data objects. The generated objects have the following fields:

  • topic: Topic name, as indicated in the coding scheme. This is a single word, without spaces (hyphens and underscores are allowed). It becomes a column name for codes in the CSV output file.
  • code: A code category name or numeric score, as described in the coding scheme.
  • confidence: Level of confidence in this code determination, expressed as a number between 0 and 100. This field is optional, and may not appear in the response.
  • detail: Reasoning for the determination of the code assigned to this topic. This field is optional, and may not appear in the response.

The CSV output file will contain a column named "tuid" (a unique ID) and a column named "generation" which will contain any transaction error messages and/or the generated JSON response. It will also contain a new column for each topic name encountered in the model's responses. The values in the topic column are the generated codes, extracted from the JSON response.

Occasionally, the model will fail to code the text properly, perhaps even producing malformed JSON. These records will be evident in the CSV file, and if you retry those records, they always seem to complete successfully. The command json single can be useful for manually retrying individual records.

Describe your coding scheme in a system file (either plain text or markdown) that is the same for every record. It will be cached, increasing performance and decreasing the number of input tokens for each record. Your coding scheme should clearly describe your topic names and the corresponding vocabulary of codes for each topic. These codes might be either category names or numeric scores. It might also include a list of decision rules for each topic, much like you might give to a fellow researcher who is helping you code study participant responses.

You could also include the following block of markdown text in your system file to provide clarity to the model about what each of the structured output fields is intended to contain.

## Code Structure

In this study, we expect to produce an array of objects, one for each topic, 
using the following structure:

| Field Name | Required? | Description |
| --- | --- | --- |
| topic | Required | The topic name, as indicated in the coding scheme. |
| code | Required | A code name or numeric score, as described in the coding scheme. |
| confidence | Optional | Level of confidence in this code determination, ranging from 0 to 100. |
| detail | Optional | Reasoning for the determination of the code assigned to this topic. |

When you place your coding scheme in the system file, your prompt file becomes much smaller. It will usually contain the specific answer(s) provided by the study participant, using template substitution variables referring to the appropriate column name(s), such as {{pid}} or {{answer}}. You might also include a specific directive, as in the following example.

## Task

The study participants were asked: "..."

In this case, participant ID {{pid}} wrote the following:

<answer>
    {{answer}}
</answer>

Please code this answer using the coding scheme provided. We expect an array of 9 objects.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package psy provides functionality that may be useful for psychology researchers.
Package psy provides functionality that may be useful for psychology researchers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL