Skip to content

Instantly share code, notes, and snippets.

@rssnyder
Last active April 29, 2024 00:53
Show Gist options
  • Save rssnyder/927c4bceff3dc4da4e5c528fc4ed7a20 to your computer and use it in GitHub Desktop.
Save rssnyder/927c4bceff3dc4da4e5c528fc4ed7a20 to your computer and use it in GitHub Desktop.
ai answers in the terminal

open ended ai prompts

insprired by https://nickherrig.com/posts/streaming-requests

type a question into your terminal, get an ai respose

write pretty much the same code as the source blog, just using the entire source command+args to craft our input

question = " ".join(argv)

make symlinks to cover all of the five w's

╭─riley@hurley ~/.local/bin  
╰─➤  ln -s who what                   
╭─riley@hurley ~/.local/bin  
╰─➤  ln -s who when
╭─riley@hurley ~/.local/bin  
╰─➤  ln -s who where
╭─riley@hurley ~/.local/bin  
╰─➤  ln -s who why  
╭─riley@hurley ~/.local/bin  
╰─➤  ln -s who how
╭─riley@hurley ~/.local/bin  
╰─➤  ll
total 625
lrwxrwxrwx 1 riley riley    3 Apr 28 17:19 how -> who
lrwxrwxrwx 1 riley riley    3 Apr 28 17:19 what -> who
lrwxrwxrwx 1 riley riley    3 Apr 28 17:19 when -> who
lrwxrwxrwx 1 riley riley    3 Apr 28 17:19 where -> who
-rwxr-xr-x 1 riley riley  610 Apr 28 17:16 who
lrwxrwxrwx 1 riley riley    3 Apr 28 17:19 why -> who

make sure these get found first in our path (if there are conflicting commands, i dont use them):

export PATH=$HOME/.local/bin:/usr/local/go/bin:$PATH

test it out

╭─riley@hurley ~
╰─➤  who won the 1996 nba finals
A blast from the past!

The Chicago Bulls, led by Michael Jordan, defeated the Seattle SuperSonics in the 1996 NBA Finals, winning the series 4-1. This was the Bulls' fourth NBA championship under Jordan's leadership.

history

abbreviated and annotated export of my shell history after completing the above work

# writing the code
code ~/.local/bin/who
# trying it out
who won the 1996 nba finals
# fixing path order
vim ~/.zshrc
# trying it out again
who won the 1996 nba finals
how can i connect to a postgres database using python
#!/bin/python3
from json import loads
from sys import argv
from requests import post
def ask(question: str):
resp = post(
"http://192.168.0.2:11434/api/generate",
stream=True,
json={
"model": "llama3",
"prompt": question
}
)
resp.raise_for_status()
return resp
if __name__ == "__main__":
question = " ".join(argv)
answer = ask(question)
for line in answer.iter_lines():
if line:
decoded_line = line.decode("utf-8")
print(loads(decoded_line).get("response", ""), end="", flush=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment