-
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.
- Loading branch information
1 parent
7ceae0d
commit 610cdb8
Showing
12 changed files
with
184 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,4 @@ | |
|
||
class ResearchNote(BaseModel): | ||
content: str | ||
title: str | ||
link: str | ||
description: str | ||
outline_items: List[int] |
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,7 @@ | ||
[ | ||
{ | ||
"topic": "Lectures on Stochastic Processes", | ||
"draft": "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": "## Random Walk\n\n### Symmetric Simple Random Walk\n\nConsider a random walk where the initial position is denoted by X0 = x and subsequent positions are given by the formula:\n\nXn+1 = Xn + ξn+1. (1.1)\n\nHere, ξi are independent and identically distributed random variables with the property:\n\nP[ξi = ±1] = 1/2.\n\nThe probabilities for this random walk are also dependent on the initial position x, and we will denote them by Px. We can think of this random walk as a fair gambling game, where at each stage, one either wins or loses a fixed amount.\n\nLet Ty be the first time n ≥ 1 when Xn = y. We define ρxy as the probability that the random walk starting at x ever reaches the position y at some future time.\n\nFirst, we will show that ρ12 = 1. This means that in this fair game, one is almost certain to eventually get ahead by one unit. This can be proven using the following three equations.\n\nThe first equation states that in the first step, the random walk either goes directly from 1 to 2 or goes from 1 to 0 and then must go from 0 to 2. Thus,\n\nρ12 = 1/2 + 1/2 * ρ02. (1.2)\n\nThe second equation states that to go from 0 to 2, the random walk must first go from 0 to 1 and then from 1 to 2. Furthermore, these two events are independent." | ||
} | ||
] |
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,42 @@ | ||
import urllib.parse | ||
from json import JSONDecodeError | ||
|
||
import aiohttp | ||
|
||
from app.services.exceptions import RequestError, ResponseError | ||
from app.services.schemas import ServiceInfo, ServiceNames, ServiceSettings | ||
from app.settings import settings | ||
|
||
wiki_search_settings = ServiceSettings(name=ServiceNames.custom, type="wiki") | ||
|
||
|
||
async def custom_search_router(service_settings: ServiceSettings, service_info: ServiceInfo): | ||
match service_settings.type: | ||
case "wiki": | ||
response = await run_search( | ||
service_info.query, "search", extract_field="match" | ||
) | ||
case _: | ||
raise RequestError(f"Unknown external search service type {service_settings.type}") | ||
return response | ||
|
||
|
||
async def run_search(query: str, endpoint: str, extract_field: str = None): | ||
if not settings.CUSTOM_SEARCH_SERVER: | ||
raise RequestError(f"Custom search server not configured") | ||
|
||
params = {"query": query} | ||
auth = aiohttp.BasicAuth(settings.CUSTOM_SEARCH_USER, settings.CUSTOM_SEARCH_PASSWORD) | ||
|
||
request_url = f"{settings.CUSTOM_SEARCH_SERVER}/{endpoint}" | ||
|
||
try: | ||
async with aiohttp.ClientSession() as session: | ||
async with session.get(request_url, params=params, auth=auth) as response: | ||
json = await response.json() | ||
except aiohttp.ClientResponseError as e: | ||
raise RequestError(f"Custom search request failed with status {e.status}") | ||
except JSONDecodeError as e: | ||
raise ResponseError(f"Could not decode custom search response as JSON: {e}") | ||
|
||
return {"text": json[extract_field]} |
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,47 @@ | ||
import asyncio | ||
from typing import List | ||
|
||
from pydantic import BaseModel | ||
|
||
from app.services.schemas import ServiceInfo, SearchData | ||
from app.services.service import get_service_response | ||
from app.settings import settings | ||
from app.services.adaptors.custom_search import wiki_search_settings | ||
|
||
|
||
async def search_wiki(queries: List[str]) -> List[SearchData]: | ||
coroutines = [_search_wiki(query) for query in queries] | ||
|
||
# Run queries in parallel | ||
results = await asyncio.gather(*coroutines) | ||
|
||
# Filter results to only unique wiki entries | ||
filtered = [] | ||
seen_text = [] | ||
for r in results: | ||
text = r.content[0] | ||
if text not in seen_text: | ||
seen_text.append(text) | ||
filtered.append(r) | ||
return filtered | ||
|
||
|
||
async def _search_wiki(query): | ||
if not settings.CUSTOM_SEARCH_SERVER: | ||
return [] | ||
|
||
service_info = ServiceInfo(query=query) | ||
response = await get_service_response(wiki_search_settings, service_info, cache=False) | ||
|
||
content = [] | ||
curr_block = "" | ||
for line in response["text"].split("\n"): | ||
curr_block += line + "\n" | ||
if len(curr_block) > settings.CONTEXT_BLOCK_SIZE: | ||
content.append(curr_block.strip()) | ||
curr_block = "" | ||
|
||
if curr_block: | ||
content.append(curr_block.strip()) | ||
|
||
return SearchData(content=content, query=query) |
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