I tried adding a AI on a batch file, but the problem is every time I write a new statement, the command overwrites all the text, hence I am not able to access previous memories.
@echo off
setlocal EnableDelayedExpansion
set "memory_file=memory.txt"
if not exist "%memory_file%" (
echo.>"%memory_file%"
)
:loop
color 7
set /p user_input=Prompt:
if /I "%user_input%"=="exit" goto end
**set "previous_memory="
for /f "usebackq delims=" %%A in ("%memory_file%") do (
set "previous_memory=!previous_memory!%%A"
)**
color a
echo { > body.json
echo "model": "meta-llama/llama-4-scout-17b-16e-instruct", >> body.json
echo "messages": [ >> body.json
echo { >> body.json
echo "role": "system", >> body.json
echo "content": "You are Julia, a helpful assistant. Here is your previous memory: !previous_memory!" >> body.json
echo }, >> body.json
echo { >> body.json
echo "role": "user", >> body.json
echo "content": "%user_input%" >> body.json
echo } >> body.json
echo ] >> body.json
echo } >> body.json
curl https://api.groq.com/openai/v1/chat/completions -s ^
-H "Content-Type: application/json" ^
-H "Authorization: Bearer gsk_***************************" ^
-d @body.json > response.json
for /f "delims=" %%A in ('powershell -Command "Get-Content response.json | ConvertFrom-Json | Select-Object -ExpandProperty choices | Select-Object -ExpandProperty message | Select-Object -ExpandProperty content"') do (
set "ai_response=%%A"
)
echo Julia: %ai_response%
**(
echo("role":"user","content":"%user_input%"),
echo("role":"assistant","content":"%ai_response%"),
)>>"%memory_file%"**
goto loop
:end
REM del body.json
REM del response.json
endlocal
Problem 1: (quibble) If set /p
receives a response of just Enter then the variable will remain unchanged
set "user_input="
set /p "user_input=Prompt: "
if not defined user_input goto loop
Problem 2: (quibble)
(
echo {
echo "model": "meta-llama/llama-4-scout-17b-16e-instruct",
....
echo }
)> body.json
will recreate body.json
without all the redirections (as you've used later...qv)
Problem 3: (a)
echo(a string
should be
echo (a string
because a cmd
quirk is that if certain symbols follow echo
then they are interpreted as part of the echo
command. (
is one of those symbols. This is often exploited as
echo/%I_may_be_empty%
which will show an empty line if I_may_be_empty
is not assigned, but if the /
is Space then the response will be the current echo
state (Echo is on/off
)
Problem 3: (b)
You are correctly using the (...)>>filename
construct to extend the file BUT the first )
closes the first (
- not the previous (
. You need to indicate to cmd
that the )
is to be treated as an ordinary character, not one with a special meaning by escaping
the character - prefixing it with a caret (^) so use ^)
here. This applies to a number of symbols, including ^
, but not %
which is escaped by a second %
.