Skip to content

Commit 09abbb4

Browse files
committedOct 19, 2024
add example for text search
1 parent 9217c2e commit 09abbb4

File tree

4 files changed

+12753
-0
lines changed

4 files changed

+12753
-0
lines changed
 

‎text-search/README.md

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Tutorial: "Text Search"
2+
3+
This tutorial guides you through the process of creating instances of an approximation nearest neighbor search of natural language content.
4+
5+
The tutorial is based on [Crime and Punishment](https://en.wikipedia.org/wiki/Crime_and_Punishment). It is a novel by the Russian author Fyodor Dostoevsky that is a psychological exploration of guilt, morality, and redemption.
6+
7+
8+
## Requirements
9+
10+
1. Install the command-line utility from source code. It requires [Golang](https://go.dev) development environment:
11+
12+
```bash
13+
go install github.com/kshard/optimum/cmd/optimum@latest
14+
```
15+
16+
2. Define environment variable with values obtained from your provider.
17+
18+
```bash
19+
export AWS_REGION=us-east-1
20+
export AWS_ACCESS_KEY_ID=XXXXXXXXXX
21+
export AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXX
22+
export HOST=https://example.com
23+
export ROLE=arn:aws:iam::000000000000:role/example-access-role
24+
```
25+
26+
3. Clone this repository, examples and tutorial section.
27+
28+
```bash
29+
git clone github.com/kshard/optimum-tutorials
30+
cd text-search
31+
```
32+
33+
## Text retrieval
34+
35+
1. Let's create new instance of `text` data structure. We call it `scap` - Sentences of Crime and Punishment. The command displays the progress of the data structure creation. Please wait until the instance is successfully provisioned.
36+
37+
```bash
38+
optimum text create -u $HOST -r $ROLE \
39+
-n scap \
40+
-j config.json
41+
42+
# Output:
43+
#
44+
# scap (vsn Njqa2b7o86O72V.0) | CREATING ...
45+
# scap (vsn Njqa2b7o86O72V.0) | STARTING ...
46+
# scap (vsn Njqa2b7o86O72V.0) | RUNNING ...
47+
# scap (vsn Njqa2b7o86O72V.0) | SUCCEEDED ...
48+
```
49+
50+
2. `sentences/crime-and-punishment.txt` dataset is a sequence of natural language sentences from the book. Upload this dataset.
51+
52+
```bash
53+
optimum text upload -u $HOST -r $ROLE \
54+
-n scap \
55+
sentences/crime-and-punishment.txt
56+
```
57+
58+
3. Commit uploaded dataset, making it available on the read path. The command displays the progress of the commit operation. Please wait until the dataset is successfully committed. Note: the commit takes about 10 min due to intentional rate limiting for text analysis.
59+
60+
```bash
61+
optimum text commit -u $HOST -r $ROLE \
62+
-n scap
63+
64+
# Output
65+
#
66+
# scap (vsn Njqa6b7o866fsZ.L) | COMMITTING ...
67+
# scap (vsn Njqa6b7o866fsZ.L) | STARTING ...
68+
# scap (vsn Njqa6b7o866fsZ.L) | RUNNING ...
69+
# scap (vsn Njqa6b7o866fsZ.L) | SUCCEEDED ...
70+
```
71+
72+
4. Check that `scap` instance is active and the update of latest version has been successful.
73+
74+
```bash
75+
optimum text list -u $HOST -r $ROLE
76+
77+
# Output
78+
#
79+
# NAME VERSION UPDATED AT | STATUS PENDING |
80+
# scap Njqa6b7o866fsZ.L 2024-08-19 06:52:38 | ACTIVE |
81+
```
82+
83+
5. `queries.txt` dataset consists of 100 questions about "Crime and Punishment":
84+
- Lines 01-15: Plot-Related Questions
85+
- Lines 16-30: Character-Focused Questions
86+
- Lines 31-50: Themes
87+
- Lines 51-70: Philosophical Questions
88+
- Lines 71-90: Symbolism and Imagery
89+
- Lines 91-99. Historical and Social Context
90+
91+
```bash
92+
optimum text query -u $HOST -r $ROLE \
93+
-n scap \
94+
queries.txt
95+
```
96+
97+
The output for each query is 10 most sentences (sha1 hashes), each is ranked with the cosine distance. You can interpret is as
98+
* `[0.0, 0.2]` Highly similar items. Use this range when you need very close matches (e.g., finding duplicate documents).
99+
* `(0.2, 0.5]` Moderately similar items. Useful when you want to find items that are related but not identical.
100+
* `(0.5, 0.8]` Weakly similar items. This range could be used for exploratory results where you want to include some diversity.
101+
* `(0.8, 1.0]` Dissimilar items. Typically, these items are unrelated, and you might filter them out unless dissimilarity is desirable (e.g., in anomaly detection).
102+
103+
A few examples below, shows typical output:
104+
105+
```
106+
Query 1 (took 67.288517ms) | cask:vYntsiSv3o4Rga6zg7wbue4ZXR/text/scap (vsn Njzn.QG1iUgv.R.2, size 12391)
107+
> How does Raskolnikov’s initial crime drive the plot of the novel?
108+
0.103614 : the novel centers around the tormented mind of rodion raskolnikov, a man driven by desperation and ideology. the weight of raskolnikov's crime becomes an unbearable burden that consumes his soul. guilt, however, proves to be a relentless force that erodes raskolnikov’s belief in his own superiority. raskolnikov’s inner conflict represents the struggle between the desire for power and the need for human connection. raskolnikov’s interactions with others are marked by a sense of disconnection and misunderstanding. raskolnikov’s oscillation between confession and concealment reflects his deep internal conflict. raskolnikov’s crime is motivated by a mixture of desperation, pride, and a misguided sense of justice. raskolnikov’s crime is both a physical act of violence and a symbolic rejection of moral law. raskolnikov’s journey from crime to punishment is a reflection of the universal human experience of sin and redemption. raskolnikov’s eventual acceptance of his guilt is a painful but necessary step towards his redemption.
109+
0.151891 : raskolnikov’s murder of the pawnbroker is both a calculated act and a fevered impulse. the novel’s pacing mirrors raskolnikov’s descent into madness, with moments of intense introspection and sudden bursts of action.
110+
...
111+
```
112+
113+
```
114+
Query 97 (took 79.000902ms) | cask:vYntsiSv3o4Rga6zg7wbue4ZXR/text/scap (vsn Njzn.QG1iUgv.R.2, size 12391)
115+
> In what ways does Dostoevsky portray the urban environment as contributing to moral decay?
116+
0.216930 : the city of st. petersburg mirrors raskolnikov’s mental state, with its oppressive atmosphere and squalor. the novel’s portrayal of st. petersburg as a city of decay and corruption reflects the moral decay of its inhabitants.
117+
0.269576 : the novel explores the consequences of living according to abstract ideals without regard for human emotions and relationships.
118+
0.292030 : the novel critiques the dehumanizing effects of poverty and capitalism. the novel’s depiction of urban life is bleak and oppressive, reflecting the dehumanizing effects of poverty and isolation.
119+
...
120+
```

‎text-search/config.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"embeddings": {
3+
"model": "amazon.titan-embed-text-v2:0",
4+
"dimension": 256
5+
},
6+
"hnsw": {
7+
"m": 8,
8+
"m0": 64,
9+
"efConstruction": 400,
10+
"surface": "cosine"
11+
}
12+
}

‎text-search/queries.txt

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
How does Raskolnikov’s initial crime drive the plot of the novel?
2+
Why does Raskolnikov choose to murder Alyona Ivanovna, the pawnbroker?
3+
How does the murder affect Raskolnikov psychologically throughout the story?
4+
What role does the subplot involving the Marmeladov family play in the main story?
5+
How does Raskolnikov’s relationship with his sister, Dunya, develop throughout the novel?
6+
Why does Raskolnikov confess to the murder at the end of the novel?
7+
How does Raskolnikov’s internal struggle between guilt and justification evolve over the course of the novel?
8+
What is the significance of Raskolnikov’s decision to kill Lizaveta as well as the pawnbroker?
9+
How does Porfiry Petrovich manipulate Raskolnikov psychologically in their encounters?
10+
How does the setting of St. Petersburg contribute to the atmosphere of the novel?
11+
How does Raskolnikov's interaction with the police develop his character arc?
12+
In what ways do dreams play a role in shaping Raskolnikov's choices throughout the novel?
13+
How do Raskolnikov's changing plans after the murder reveal his inner conflict?
14+
What role does Razumikhin play in balancing the dark psychological elements of the plot?
15+
How does Dostoevsky use suspense and pacing to intensify the psychological tension in the story?
16+
What motivates Raskolnikov to commit the murder beyond financial need?
17+
How does Sonya’s character represent a moral compass in contrast to Raskolnikov?
18+
What role does Dunya play in Raskolnikov’s sense of family and duty?
19+
In what ways is Razumikhin a foil to Raskolnikov’s character?
20+
What does Raskolnikov's relationship with his mother, Pulcheria Alexandrovna, reveal about his personality?
21+
How does the character of Svidrigailov contribute to the theme of moral ambiguity?
22+
In what ways is Svidrigailov a double or mirror to Raskolnikov?
23+
How does Porfiry’s cat-and-mouse game with Raskolnikov drive the tension in the novel?
24+
How does Raskolnikov’s worldview clash with that of Sonya, and how does this shape his character development?
25+
How does Luzhin represent the corrupting influence of wealth and power?
26+
Why does Sonya remain loyal to Raskolnikov, even after learning of his crime?
27+
What does the relationship between Raskolnikov and Sonya suggest about redemption?
28+
How does Dunya's engagement to Luzhin illustrate her character's values and strength?
29+
What drives Svidrigailov’s obsession with Dunya?
30+
How does Raskolnikov’s alienation from society reflect his personality and inner turmoil?
31+
How does the theme of guilt and conscience permeate the novel?
32+
In what ways does Dostoevsky explore the theme of justice in the novel?
33+
How does *Crime and Punishment* examine the idea of "superman theory" through Raskolnikov’s beliefs?
34+
What role does poverty play in shaping the characters' lives and decisions?
35+
How does the novel contrast rational thought with emotion and intuition?
36+
How does Dostoevsky critique utilitarianism through Raskolnikov’s justification of his crime?
37+
In what ways does *Crime and Punishment* address the theme of alienation?
38+
How does Dostoevsky use the motif of suffering to develop the novel’s moral and philosophical themes?
39+
What does the novel suggest about the possibility of redemption and atonement?
40+
How does *Crime and Punishment* depict the struggle between good and evil within an individual?
41+
How does Dostoevsky portray the legal and criminal justice system in the novel?
42+
How does the novel explore the theme of free will versus determinism?
43+
What role does fate play in shaping the events of the novel?
44+
How does the novel depict the consequences of pride and hubris?
45+
How does Dostoevsky use Raskolnikov's crime to explore moral relativism?
46+
How does Dostoevsky portray mental illness and psychological breakdown in the novel?
47+
In what ways does the novel explore the theme of nihilism and its consequences?
48+
How does *Crime and Punishment* reflect on the role of suffering in achieving self-awareness or salvation?
49+
What does the novel suggest about the nature of evil and its roots in human nature?
50+
How does the novel explore the tension between intellectualism and morality?
51+
In what ways does Raskolnikov’s crime reflect Nietzschean philosophy or the idea of a “superman”?
52+
How does Dostoevsky challenge utilitarianism with Raskolnikov's justifications for murder?
53+
How does Raskolnikov’s theory of extraordinary men lead to his downfall?
54+
How does the novel explore the relationship between law and morality?
55+
How does Dostoevsky's portrayal of suffering align with Christian existentialism?
56+
What role does religious symbolism play in Raskolnikov’s journey toward redemption?
57+
How does Sonya’s faith challenge Raskolnikov’s existential despair?
58+
What does the novel suggest about the nature of free will and moral responsibility?
59+
How does the novel engage with the philosophical ideas of nihilism?
60+
How does the novel critique the Enlightenment emphasis on reason and individualism?
61+
How does Dostoevsky explore the theme of God and forgiveness in *Crime and Punishment*?
62+
How does Raskolnikov’s inner dialogue reflect the philosophical conflict between rational egoism and altruism?
63+
What does Dostoevsky suggest about the relationship between morality and religion in the novel?
64+
How does the novel explore the psychological and moral implications of crime beyond legal punishment?
65+
In what ways does the novel question the possibility of true justice in human society?
66+
What does Dostoevsky suggest about the existence of absolute moral truths versus relative ones?
67+
How does the novel engage with the existential idea that life is inherently meaningless?
68+
How does Raskolnikov’s attempt to play God contribute to his psychological decline?
69+
How does the novel challenge the reader's understanding of morality and justice?
70+
What is the significance of Raskolnikov’s final epiphany in relation to philosophical themes of salvation?
71+
What is the symbolic meaning of the recurring image of blood in the novel?
72+
How does the setting of St. Petersburg function symbolically in the novel?
73+
What is the significance of dreams in *Crime and Punishment*, particularly Raskolnikov’s dream of the horse?
74+
How does the symbol of the axe represent Raskolnikov’s inner conflict?
75+
What is the symbolic importance of the cross that Sonya gives to Raskolnikov?
76+
How does Dostoevsky use light and darkness to reflect the characters’ inner states?
77+
How does the weather and physical environment in the novel mirror Raskolnikov’s mental and emotional condition?
78+
What does the pawnshop symbolize in the context of the novel’s themes of poverty and desperation?
79+
How does the use of colors like yellow (Alyona’s apartment, Sonya’s hair) contribute to the novel’s atmosphere?
80+
How does the image of the bridge serve as a metaphor for transition or liminality in the novel?
81+
How does the portrayal of alcohol in the novel serve as a symbol of escape and despair?
82+
What does Raskolnikov’s physical illness after the murder symbolize?
83+
How does the image of the horse in Raskolnikov’s dream reflect his internal struggle with guilt and morality?
84+
What is the significance of the Lazarus story, which Sonya reads to Raskolnikov?
85+
How does the image of Raskolnikov’s room reflect his isolation and psychological turmoil?
86+
How does the theme of doubling (Raskolnikov/Svidrigailov) function symbolically in the novel?
87+
How do the streets of St. Petersburg act as a symbolic reflection of Raskolnikov's mental state?
88+
How is money portrayed as a symbol of corruption and moral decay in the novel?
89+
What role does water symbolism (rain, rivers) play in shaping the novel’s mood and themes?
90+
How does Dostoevsky use the image of imprisonment to explore the theme of internal punishment?
91+
How does the novel reflect the socio-economic conditions of 19th-century Russia?
92+
In what ways does *Crime and Punishment* critique the social inequality and poverty of the time?
93+
How do the characters’ struggles reflect broader societal issues in pre-revolutionary Russia?
94+
What role does class play in shaping the moral decisions of the characters?
95+
How does Dostoevsky use the legal system in the novel to comment on Russian society?
96+
How does the novel explore the tension between Westernization and traditional Russian values?
97+
In what ways does Dostoevsky portray the urban environment as contributing to moral decay?
98+
How does the novel address the conflict between modernity and traditionalism in Russia?
99+
What is the significance of Dostoevsky’s personal experiences with crime and punishment in shaping the

‎text-search/sentences/crime-and-punishment.txt

+12,522
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.