forked from tryAGI/LangChain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadmeTests.cs
213 lines (182 loc) · 8.32 KB
/
ReadmeTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using LangChain.Databases;
using LangChain.Databases.InMemory;
using LangChain.Docstore;
using LangChain.Indexes;
using LangChain.Providers.OpenAI;
using LangChain.Sources;
using LangChain.TextSplitters;
using LangChain.VectorStores;
using static LangChain.Chains.Chain;
namespace LangChain.IntegrationTests;
[TestFixture]
public class ReadmeTests
{
[Explicit]
[Test]
public async Task Readme()
{
var gpt4 = new Gpt4Model(
Environment.GetEnvironmentVariable("OPENAI_API_KEY") ??
throw new InconclusiveException("OPENAI_API_KEY is not set"));
var index = await InMemoryVectorStore.CreateIndexFromDocuments(gpt4, new[]
{
"I spent entire day watching TV",
"My dog name is Bob",
"This ice cream is delicious",
"It is cold in space"
}.ToDocuments());
var chain = (
Set("What is the good name for a pet?", outputKey: "question") |
RetrieveDocuments(index, inputKey: "question", outputKey: "documents") |
StuffDocuments(inputKey: "documents", outputKey: "context") |
Template("""
Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
{context}
Question: {question}
Helpful Answer:
""", outputKey: "prompt") |
LLM(gpt4, inputKey: "prompt", outputKey: "pet_sentence")) >>
Template("""
Human will provide you with sentence about pet. You need to answer with pet name.
Human: My dog name is Jack
Answer: Jack
Human: I think the best name for a pet is "Jerry"
Answer: Jerry
Human: {pet_sentence}
Answer:
""", outputKey: "prompt") |
LLM(gpt4, inputKey: "prompt", outputKey: "text");
var result = await chain.Run(resultKey: "text");
result.Should().Be("Bob");
}
/// <summary>
/// Price to run from zero(create embedding and request to LLM): 0,015$
/// Price to re-run if database is exists: 0,0004$
/// </summary>
/// <exception cref="InconclusiveException"></exception>
[Explicit]
[Test]
public async Task RagWithOpenAiUsingChains()
{
var gpt35 = new Gpt35TurboModel(
Environment.GetEnvironmentVariable("OPENAI_API_KEY") ??
throw new InconclusiveException("OPENAI_API_KEY is not set"));
// Create database with embeddings from Harry Potter book pdf
if (!File.Exists("vectors.db"))
{
await using var stream = H.Resources.HarryPotterBook1_pdf.AsStream();
var documents = await PdfPigPdfSource.FromStreamAsync(stream);
await SQLiteVectorStore.CreateIndexFromDocuments(
embeddings: gpt35,
documents: documents,
filename: "vectors.db",
tableName: "vectors",
textSplitter: new RecursiveCharacterTextSplitter(
chunkSize: 200,
chunkOverlap: 50));
}
var vectorStore = new SQLiteVectorStore("vectors.db", "vectors", gpt35);
var index = new VectorStoreIndexWrapper(vectorStore);
var chain =
// set the question
Set("Who was drinking a unicorn blood?", outputKey: "question") |
// take 5 most similar documents
RetrieveSimilarDocuments(index, inputKey: "question", outputKey: "documents", amount: 5) |
// combine documents together and put them into context
CombineDocuments(inputKey: "documents", outputKey: "context") |
// replace context and question in the prompt with their values
Template(@"Use the following pieces of context to answer the question at the end.
If the answer is not in context then just say that you don't know, don't try to make up an answer.
Keep the answer as short as possible.
{context}
Question: {question}
Helpful Answer:") |
// send the result to the language model
LargeLanguageModel(gpt35);
var result = await chain.Run("text");
Console.WriteLine($"LLM answer: {result}");
Console.WriteLine($"Total usage: {gpt35.TotalUsage}");
}
/// <summary>
/// Price to run from zero(create embeddings and request to LLM): 0,015$
/// Price to re-run if database is exists: 0,0004$
/// </summary>
/// <exception cref="InconclusiveException"></exception>
[Explicit]
[Test]
public async Task RagWithOpenAiUsingAsyncMethods()
{
var gpt35 = new Gpt35TurboModel(
Environment.GetEnvironmentVariable("OPENAI_API_KEY") ??
throw new InconclusiveException("OPENAI_API_KEY is not set"));
if (!File.Exists("vectors.db"))
{
var documents = await PdfPigPdfSource.FromUriAsync(
new Uri("https://canonburyprimaryschool.co.uk/wp-content/uploads/2016/01/Joanne-K.-Rowling-Harry-Potter-Book-1-Harry-Potter-and-the-Philosophers-Stone-EnglishOnlineClub.com_.pdf"));
await SQLiteVectorStore.CreateIndexFromDocuments(
embeddings: gpt35,
documents: documents,
filename: "vectors.db",
tableName: "vectors",
textSplitter: new RecursiveCharacterTextSplitter(
chunkSize: 200,
chunkOverlap: 50));
}
var database = new SQLiteVectorStore(
filename: "vectors.db",
tableName: "vectors",
embeddings: gpt35);
const string question = "Who was drinking a unicorn blood?";
var similarDocuments = await database.GetSimilarDocuments(question, amount: 5);
var answer = await gpt35.GenerateAsync(
$"""
Use the following pieces of context to answer the question at the end.
If the answer is not in context then just say that you don't know, don't try to make up an answer.
Keep the answer as short as possible.
{similarDocuments.AsString()}
Question: {question}
Helpful Answer:
""", CancellationToken.None).ConfigureAwait(false);
Console.WriteLine($"LLM answer: {answer}"); // The cloaked figure.
Console.WriteLine($"Total usage: {gpt35.TotalUsage}");
}
[Explicit]
[Test]
public async Task SimpleDocuments()
{
// https://www.together.ai/blog/embeddings-endpoint-release
// var together = new OpenAiModel(new OpenAiConfiguration
// {
// ApiKey = Environment.GetEnvironmentVariable("TOGETHER_API_KEY") ??
// throw new InconclusiveException("TOGETHER_API_KEY is not set"),
// Endpoint = "api.together.xyz",
// ModelId = "togethercomputer/Qwen-7B",
// EmbeddingModelId = "togethercomputer/m2-bert-80M-32k-retrieval",
// });
var gpt35 = new Gpt35TurboModel(
Environment.GetEnvironmentVariable("OPENAI_API_KEY") ??
throw new InconclusiveException("OPENAI_API_KEY is not set"));
var database = await InMemoryVectorStore.CreateIndexFromDocuments(gpt35, new[]
{
"I spent entire day watching TV",
"My dog name is Bob",
"This ice cream is delicious",
"It is cold in space"
}.ToDocuments());
const string question = "What is the good name for a pet?";
var similarDocuments = await database.Store.GetSimilarDocuments(question, amount: 1);
var petNameResponse = await gpt35.GenerateAsync(
$"""
Human will provide you with sentence about pet. You need to answer with pet name.
Human: My dog name is Jack
Answer: Jack
Human: I think the best name for a pet is "Jerry"
Answer: Jerry
Human: {similarDocuments.AsString()}
Answer:
""", CancellationToken.None).ConfigureAwait(false);
Console.WriteLine($"LLM answer: {petNameResponse}");
Console.WriteLine($"Total usage: {gpt35.TotalUsage}");
petNameResponse.ToString().Should().Be("Bob");
}
}