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

How to support context #9

Closed
IamJasonBoy opened this issue Apr 27, 2023 · 11 comments
Closed

How to support context #9

IamJasonBoy opened this issue Apr 27, 2023 · 11 comments

Comments

@IamJasonBoy
Copy link

How to support context and what is the format of file content when I use this method

cat bito.txt | bito
@macky3099
Copy link
Collaborator

macky3099 commented Apr 27, 2023

Hi @IamJasonBoy,

You can use -p flag along with cat command to provide the context. So from your example above, it will look like as follows:
cat bito.txt | bito -p prompt.txt where prompt.txt will contain your prompt.

You can check for more such options and examples in the Readme under "Bito CLI commands" section here: https://github.com/gitbito/CLI#bito-cli-commands

@IamJasonBoy
Copy link
Author

IamJasonBoy commented Apr 27, 2023

I don't quite understand, if I ask a question once. When asked the second time. The text for the second question is stored in bito.txt. How to store the first question

cat bito.txt | bito -p prompt.txt

bito.txt -> Save this text
prompt.txt -> What format are historical questions and answers stored in?

@macky3099
Copy link
Collaborator

@IamJasonBoy if I understood correctly you want to save multiple questions or have multiple prompts?

@IamJasonBoy
Copy link
Author

It's contextual communication, and my second question relies on the first one.
For example,
when asking a question for the first time, write a fifty word article,
the second question to 100 words.
How to convey the content of the previous text

@bitoandy
Copy link
Contributor

bitoandy commented Apr 27, 2023

Hi @IamJasonBoy ,

Currently Bito CLI does not support context out of the box, except for in chat mode. When used in non chat mode with -p option, each call uses the prompt plus the input provided for evaluating instructions in the prompt file.

We are working on supporting context, but it's not available today. We will update you once the functionality is available.

@bitoandy
Copy link
Contributor

bitoandy commented Apr 27, 2023

Hi @IamJasonBoy ,

Till we have the context support implemented natively for non chat mode and make it available, one can write a shell script which maintains context and uses Bito CLI with it.

Given below is an example shell script rwc.sh (run with context) showing how one can maintain and use context with Bito CLI (its an example provided without any warranty or guarantee, may have bugs :), one can make it better by adding validations, better way of handling input and parameter processing. Please feel free to use it, modify it based on your needs).
It takes prompt file and context file as inputs. The context is created and maintained in the context file eg:testmac ~ % echo "55" | ./rwc.sh pmultiplication.txt myctx1.txt where pmultiplication.txt is prompt file and myctx1.txt is the context file. Hope this example helps with your automation in the interim.

#!/bin/bash

# If you want to debug or print each command use -x
# else set it to +x
set +x

# Number of lines to use as context from the context file
# Here we are limiting the context from the context file to max
# of 512 lines for its use as context while executing prompt
CONTEXT_LINES_TO_USE=512

# Bito CLI 
BITO_CLI=`which bito`

# Temporary file to create prompt with context
# based on the prompt provided plus the existing context
PROMP_WITH_CONTEXT=ctxprompt.$$
# Temporary output file
OUTPUT_FILE=.op.$$

# from command line get first parameter to use as prompt file
PROMPT_FILE=$1
# Variable for file to maintain context
CONTEXT_FILE=$2

# create an input file based on the data provided in input which is piped
INPUT_FILE_DATA=$(cat)

# Create temporary files
touch $CONTEXT_FILE
touch $OUTPUT_FILE

# Create prompt to execute using prompt and context using
# fixed lines from previous context
echo "#context-start#" >> $PROMP_WITH_CONTEXT
tail -$CONTEXT_LINES_TO_USE $CONTEXT_FILE >> $PROMP_WITH_CONTEXT
echo "#context-end#" >> $PROMP_WITH_CONTEXT
echo "previous context is provided within '#context-start#' and '#context-end#' where User: means input provided by the user and Bito CLI: means the response. Please answer:" >>  $PROMP_WITH_CONTEXT
cat $PROMPT_FILE >> $PROMP_WITH_CONTEXT

# execute the create prompt
echo $INPUT_FILE_DATA |$BITO_CLI -p $PROMP_WITH_CONTEXT > $OUTPUT_FILE

# print the output 
cat $OUTPUT_FILE

# Update the context in the context file.
# The context is maintained as
# User: 
# content of the prompt with input
# Bito CLI:
# output from Bito CLI
echo "User:" >> $CONTEXT_FILE

# Check if {{%input%}} is used in prompt file
# if yes replace it with the input provided
# if not append the input after the prompt
if grep -q "{{%input%}}" "$PROMPT_FILE"; then
       sed "s/{{%input%}}/$(echo $INPUT_FILE_DATA| sed 's/$/\\n/' | tr -d '\n')/g" $PROMPT_FILE >> $CONTEXT_FILE
else
	cat $PROMPT_FILE >> $CONTEXT_FILE
	echo $INPUT_FILE_DATA >> $CONTEXT_FILE
fi
# Add the output to the context file
echo "Bito CLI:" >> $CONTEXT_FILE

# remove all the temporary files
cat $OUTPUT_FILE >> $CONTEXT_FILE

# Remove temporary files
rm $OUTPUT_FILE $PROMP_WITH_CONTEXT $INPUT_FILE

Given below is an example run:

testmac ~ % echo "55" | ./rwc.sh pmultiplication.txt myctx1.txt
Sure, here's the multiplication table for 55:

55 x 1 = 55
55 x 2 = 110
55 x 3 = 165
55 x 4 = 220
55 x 5 = 275
55 x 6 = 330
55 x 7 = 385
55 x 8 = 440
55 x 9 = 495
55 x 10 = 550

testmac ~ % echo "awk" | ./rwc.sh pwhatis.txt myctx1.txt
awk is a command-line tool used for text processing and searching. It is typically used for manipulating text files and data streams, and is designed to be highly flexible and customizable.

testmac ~ % echo "what are the questions i have asked till now" | ./rwc.sh emptyprompt.txt myctx1.txt 
From the provided context, you have asked for:

1. "print multiplication table for the following number: 55"
2. "Please answer what is following: awk"
testmac ~ % 

Given below are the prompt files use in the above example run:
pmultiplication.txt

print multiplication table for the following number:

pwhatis.txt

Please answer what is following:
{{%input%}}

emptyprompt.txt is an empty file

Given below is the content of the context file myctx1.txt created based on the example run mentioned above:

testmac ~ % cat myctx1.txt 
User:
print multiplication table for the following number:

55
Bito CLI:
Sure, here's the multiplication table for 55:

55 x 1 = 55
55 x 2 = 110
55 x 3 = 165
55 x 4 = 220
55 x 5 = 275
55 x 6 = 330
55 x 7 = 385
55 x 8 = 440
55 x 9 = 495
55 x 10 = 550

User:
Please answer what is following:
awk

Bito CLI:
awk is a command-line tool used for text processing and searching. It is typically used for manipulating text files and data streams, and is designed to be highly flexible and customizable.

User:
what are the questions i have asked till now
Bito CLI:
From the provided context, you have asked for:

1. "print multiplication table for the following number: 55"
2. "Please answer what is following: awk"

With a multiline file as input:
cmdlist.txt contains list of commands

testmac ~ % cat cmdlist.txt 
awk
lex
grep
sed

pexplaincommand.txt contains following prompt

testmac ~ % cat pexplaincommand.txt
Please explain each of the following:
{{%input%}}

Here's the output of the shell script

testmac ~ % cat t.txt | ./rwc.sh pwhatis.txt myctx1.txt
Sure, here's an explanation of each of the following terms:

1. awk - A command-line tool used for text processing and searching in Unix/Linux systems. It is designed to be highly flexible and customizable, and is typically used for manipulating text files and data streams.

2. lex - A program generator used to generate lexical analyzers (tokenizers) for text processing. It takes a set of regular expressions as input and generates code in C, which can then be compiled and run.

3. grep - A command-line tool used to search for patterns in text files. It is typically used to search for specific words or phrases within a file or directory, and can also be used to search recursively.

4. sed - A command-line tool used for text processing and editing in Unix/Linux systems. It is typically used to perform simple text transformations on an input file (or stream of data), such as search-and-replace operations or line deletions.

Hope this helps

@IamJasonBoy
Copy link
Author

Thank you for your answer, although I didn't understand

@minghua-li
Copy link

Thank you for your answer, although I didn't understand

他的意思应该是将每次的回答前面加上"Bito CLI:",添加到context文件的末尾,作为下一次询问的context.

@bitoandy
Copy link
Contributor

@IamJasonBoy ,
With the latest version 3.1 launched today, context in non-interactive mode is now supported. @macky3099 will provide some examples in comments later with some examples

@macky3099
Copy link
Collaborator

Hello @IamJasonBoy

You can now use -c flag along with cat, -p and -f flags to provide context in the non-interactive mode.

You just need to provide some filename to -c flag as an argument and keep using that same filename for -c for all your further requests to Bito.

Here is how you can do it:

  1. cat prompt.txt | bito -c context.txt
  2. bito -p prompt.txt -f file.txt -c context.txt
  3. echo "your query" | bito -c context.txt

I am also attaching a screenshot that shows the above examples in action:

ScreenshotGithub

Let us know if you face any issues.

Thanks!

@macky3099
Copy link
Collaborator

@IamJasonBoy also, do let us know if this solves your problem and if we can close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants