-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from VikParuchuri/dev
Add wiki retrieval
- Loading branch information
Showing
23 changed files
with
336 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[ | ||
{ | ||
"topic": "Lectures on Stochastic Processes", | ||
"research notes\n": "* ```\nA popular random walk model is that of a random walk on a regular lattice, where at each step the location jumps to another site according to some probability distribution. In a simple random walk, the location can only jump to neighboring sites of the lattice, forming a lattice path. In a simple symmetric random walk on a locally finite lattice, the probabilities of the location jumping to each one of its immediate neighbors are the same.\n```\n* ```\nA stochastic process {Xn}n∈N0 is called a simple random walk if\n1. X0 = 0,\n2. the increment Xn+1 − Xn is independent of (X0, X1, . . . , Xn) for each n ∈ N0, and\n3. the increment Xn+1 − Xn has the coin-toss distribution, i.e.\nP[Xn+1 − Xn = 1] = P[Xn+1 − Xn = −1] = 1\n2 .\nFor the sequence {γn}n∈N, given by Theorem 3.2, define the following, new, sequence {ξn}n∈N\nof random variables:\nξn =\n{\n1, γn ≥ 1\n2\n−1, otherwise.\nThen, we set\nX0 = 0, Xn =\nn∑\nk=1\nξk, n ∈ N.\nIntuitively, we use each ξn to emulate a coin toss and then define the value of the process X at\ntime n as the cumulative sum of the first n coin-tosses.\n```", | ||
"draft\n": "Random walk\n1.1\nSymmetric simple random walk\nLet X0 = x and\nXn+1 = Xn + ξn+1.\n(1.1)\nThe ξi are independent, identically distributed random variables such that\nP[ξi = ±1] = 1/2.\nThe probabilities for this random walk also depend on\nx, and we shall denote them by Px. We can think of this as a fair gambling\ngame, where at each stage one either wins or loses a fixed amount.\nLet Ty be the first time n ≥ 1 when Xn = y. Let ρxy = Px[Ty < ∞] be the\nprobability that the walk starting at x ever gets to y at some future time.\nFirst we show that ρ12 = 1. This says that in the fair game one is almost\nsure to eventually get ahead by one unit. This follows from the following three\nequations.\nThe first equation says that in the first step the walk either goes from 1 to\n2 directly, or it goes from 1 to 0 and then must go from 0 to 2. Thus\nρ12 = 1\n2 + 1\n2ρ02.\n(1.2)\nThe second equation says that to go from 0 to 2, the walk has to go from\n0 to 1 and then from 1 to 2. Furthermore, these two events are independent.", | ||
"markdown": "### 1.1 Symmetric Simple Random Walk\nA random walk is a mathematical model that describes the path of a randomly moving object in discrete time steps. \n\nLet's start with the simplest form of a random walk, called the symmetric simple random walk. In this case, the object can only move one step to the left or one step to the right with equal probability at each time step. \n\nWe denote the current position of the object at time $n$ as $Xn$. The position at time $n+1$ is determined by adding an increment $ξn+1$ to the current position Xn. The increments ξn+1 are independent, identically distributed random variables, where P[ξn+1 = ±1] = 1/2.\n\nTo understand the behavior of the symmetric simple random walk, we introduce the concept of hitting time. Let Ty be the first time n ≥ 1 when Xn = y. The hitting time represents the number of steps it takes for the random walk to reach a specific position y.\n\nWe are interested in the probability that the random walk starting at position x ever reaches position y at some future time. We denote this probability as ρxy = Px[Ty < ∞].\n\nTo calculate ρxy, we can use a recursive approach. Let's consider the case where y = 1. We want to find ρ12, the probability that the random walk starting at position x ever reaches position 1.\n\nWe can break down the calculation of ρ12 into three equations:\n\n1. The first equation states that in the first step, the walk either goes directly from 1 to 2 or goes from 1 to 0 and then from 0 to 2. Therefore, we have:\n\n ρ12 = 1/2 + 1/2 * ρ02. (1.1)\n\n2. The second equation states that to go from 0 to 2, the walk must go from 0 to 1 and then from 1 to 2. Furthermore, these two events are independent.\n\n3. The third equation states that to go from 0 to 2, the walk must go from 0 to 1 and then from 1 to 2. Furthermore, these two events are independent.\n\nNow, let's solve these equations to find ρ12.\n\n```\nρ12 = 1/2 + 1/2 * ρ02\n```\n\nTo solve this equation, we need to find ρ02. We can use the same approach of breaking down the calculation into three equations:\n\n1. The first equation states that in the first step, the walk either goes directly from 0 to 1 or goes from 0 to -1 and then from -1 to 1. Therefore, we have:\n\n ρ02 = 1/2 + 1/2 * ρ-12. (1.2)\n\n2. The second equation states that to go from -1 to 1, the walk must go from -1 to 0 and then from 0 to 1. Furthermore, these two events are independent.\n\n3. The third equation states that to go from -1 to 1, the walk must go from -1 to 0 and then from 0 to 1. Furthermore, these two events are independent.\n\nBy solving these equations iteratively, we can find the value of ρ12.\n\n### Example\n\nLet's consider a symmetric simple random walk starting at position 0. We want to calculate the probability of reaching position 2 at some future time.\n\nUsing the recursive approach, we can calculate ρ02 as follows:\n\n```\nρ02 = 1/2 + 1/2 * ρ-12\n```\n\nTo find ρ-12, we repeat the process:\n\n```\nρ-12 = 1/2 + 1/2 * ρ-22\n```\n\nContinuing this process, we eventually find the value of ρ02.\n\nNow, let's substitute the value of ρ02 into the equation for ρ12:\n\n```\nρ12 = 1/2 + 1/2 * ρ02\n```\n\nBy solving this equation, we can determine the probability of reaching position 2 starting from position 0.\n\n" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import json | ||
import os | ||
from collections import OrderedDict | ||
from typing import AsyncGenerator, List, get_args | ||
|
||
from app.course.schemas import ResearchNote | ||
from app.llm.llm import GenerationSettings, generate_response | ||
from app.llm.prompts import build_prompt, render_research_notes | ||
from app.settings import settings | ||
|
||
rewrite_settings = GenerationSettings( | ||
temperature=.6, | ||
max_tokens=6000, | ||
timeout=1200, | ||
prompt_type="rewrite" | ||
) | ||
|
||
|
||
def rewrite_prompt( | ||
topic: str, | ||
draft: str, | ||
include_examples: bool, | ||
research_notes: List[ResearchNote] | None = None, | ||
) -> str: | ||
with open(os.path.join(settings.EXAMPLE_JSON_DIR, "rewrite.json")) as f: | ||
examples = json.load(f) | ||
|
||
items = [("topic", topic)] | ||
|
||
research_notes_exist = research_notes is not None and len(research_notes) > 0 | ||
if research_notes_exist: | ||
research_content = render_research_notes(research_notes) | ||
items.append(("research notes\n", research_content)) | ||
|
||
items.append(("draft\n", draft)) | ||
|
||
input = OrderedDict(items) | ||
|
||
prompt = build_prompt( | ||
"rewrite", | ||
input, | ||
examples, | ||
include_examples=include_examples, | ||
topic=topic, | ||
research_notes=research_notes_exist, | ||
) | ||
return prompt | ||
|
||
|
||
async def generate_rewrite( | ||
topic: str, | ||
draft: str, | ||
revision: int, | ||
research_notes: List[ResearchNote] | None = None, | ||
include_examples: bool = True, | ||
cache: bool = True, | ||
) -> str: | ||
prompt = rewrite_prompt( | ||
topic, | ||
draft, | ||
include_examples, | ||
research_notes, | ||
) | ||
|
||
text = await generate_response(prompt, rewrite_settings, cache=cache, revision=revision) | ||
|
||
return text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.