#!/usr/bin/env python3

import os
import requests

LLAMA_API_KEY = os.environ["LLAMA_API_KEY"]

ENDPOINT = "http://boss-baby.mit.edu:8080/completion"

make_data = lambda prompt: {
   "api_key": LLAMA_API_KEY,
   "cache_prompt": False,
   "frequency_penalty": 0,
   "grammar": "",
   "image_data": [],
   "min_p": 0.05,
   "mirostat": 0,
   "mirostat_eta": 0.1,
   "mirostat_tau": 5,
   "n_predict": 400,
   "n_probs": 0,
   "presence_penalty": 0,
   "prompt": prompt,
   "repeat_last_n": 256,
   "repeat_penalty": 1.18,
   "slot_id": -1,
   "stop": ["</s>", "Llama:", "User:"],
   "stream": False,
   "temperature": 0.7,
   "tfs_z": 1,
   "top_k": 40,
   "top_p": 0.95,
   "typical_p": 1,
}

headers = {
   "Authorization": f"Bearer {LLAMA_API_KEY}",
   "Content-Type": "application/json",
}

_default_summarize_question_format = """
Page title: {title}

Content:

``````md
{content}
``````

---

Brief (at most 200 characters) plaintext summary of page:
""".strip("\n")

def summarize(title: str, content: str,
   question_format: str=_default_summarize_question_format
) -> str:
   """Takes in a page title and content, and summarizes it
   """
   question = question_format.format(
      title=title,
      content=content.strip("\n")
   )
   data = make_data(question)
   r = requests.post(ENDPOINT, json=data, headers=headers)
   #print(r.status_code)
   summary = "error"
   try:
      j = r.json()
      #print(f"{j=}")
      summary = j["content"]
   except:
      try:
         s = r.text
         #print(f"{s=}")
         summary = s
      except:
         pass
   return summary.strip()

if __name__ == "__main__":
   summary = summarize(
      "Student Information Processing Board", """
**The Student Information Processing Board** (**SIPB**) is a student group at the Massachusetts Institute of Technology (MIT) that helps students access computing resources and use them effectively.

## History

SIPB was founded in 1969 by Bob Frankston, Gary Gut, David Burmaster, and Ed Fox. At the time, computers in universities were still expensive resources reserved for funded research projects. Through an arrangement with the MIT administration, SIPB administered student accounts on university-owned computers. This allowed students access to MIT's timeshared computers when computers otherwise cost millions of dollars and were as big as an entire room.

In 1983, MIT launched Project Athena, an initiative to create a distributed computing system for educational use. SIPB was instrumental in the creation of Project Athena and helped to provide MIT students with access to computing resources for independent projects. Project Athena led to several important Unix technologies such as X11, instant messaging, and network filesystems.

SIPB set up a Web server at `www.mit.edu` in 1993, when the number of public web servers was roughly 100 and long before university Web sites became common. When MIT finally did set up an official website, it was at `web.mit.edu`.

## Projects

SIPB has been instrumental for funding technical software projects that benefit the MIT community. These have included:

* scripts.mit.edu, a dynamic web hosting service
* MIT Minecraft, a virtual MIT campus in Minecraft[6]
* CourseRoad, a course planning aid for MIT students
* Debathena, a set of software packages for Athena clients
* XVM, a virtual machine service
* BarnOwl, an instant messaging client
""")
   print(summary)
