-
Notifications
You must be signed in to change notification settings - Fork 1
wip: Integration #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wip: Integration #23
Changes from 30 commits
ff3fe68
38a3bf9
3fb6fd0
a4c7294
d38c5f0
dc4501a
42cdcc5
13bc12e
4933a9a
b23833b
da1017b
41ff046
4e69697
10103c6
0ee6ed5
7a2c955
0a5e2be
332e3dc
6225fcc
4877807
da9859d
291aaaf
936d83e
1b88437
3e0b8f4
edf5eb2
63baf2b
56a7b8c
8e05473
8276e35
22b04d0
10f6b21
bfbd245
7b6ba0a
0428f87
8104dde
fbc4591
caecfd1
5c0b4d0
8833af7
9ee8a32
b2357e3
b518abf
69800b0
7803649
ff1fcab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1 @@ | ||
| from .api import * | ||
| # from .haystack2beta_tutorial_InMemoryEmbeddingRetriever import * |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
| from fastapi.staticfiles import StaticFiles | ||
| from fastapi import FastAPI | ||
|
|
||
| # from .rag import rag_pipeline | ||
| from .rag import embedder, retriever, prompt_builder, llm, answer_builder | ||
| from haystack import Document | ||
|
|
||
|
|
@@ -17,55 +16,61 @@ | |
|
|
||
| @app.get("/") | ||
| async def root(): | ||
| return RedirectResponse(url="/frontend/dist", status_code=302) | ||
| # return RedirectResponse(url="/frontend/dist", status_code=308) | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return {} | ||
|
|
||
|
|
||
| @app.get("/api") | ||
| async def api(q): | ||
| async def api(q, top_k = 3, lang = 'en'): | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if not lang in ['en', 'de']: | ||
| raise Exception("language must be 'en' or 'de'") | ||
|
|
||
| embedder, retriever, prompt_builder, llm, answer_builder | ||
| print(f"{q=}") | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| print(f"{top_k=}") | ||
| print(f"{lang=}") | ||
|
|
||
| # query = "How many languages are there?" | ||
| query = Document(content=q) | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| result = embedder.run([query]) | ||
| queryEmbedded = embedder.run([query]) | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| queryEmbedding = queryEmbedded['documents'][0].embedding | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| results = retriever.run( | ||
| query_embedding=list(result['documents'][0].embedding), | ||
| retrieverResults = retriever.run( | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| query_embedding=list(queryEmbedding), | ||
| filters=None, | ||
| top_k=None, | ||
| top_k=top_k, | ||
| scale_score=None, | ||
| return_embedding=None | ||
| ) | ||
| # .run( | ||
| # result['documents'][0].embedding | ||
| # ) | ||
|
|
||
| prompt = prompt_builder.run(documents=results['documents'])['prompt'] | ||
| print("retriever results:") | ||
|
||
| for retrieverResult in retrieverResults: | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| print(retrieverResult) | ||
|
||
|
|
||
| response = llm.run(prompt=prompt, generation_kwargs=None) | ||
| # reply = response['replies'][0] | ||
| promptBuilder = prompt_builder[lang] | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| promptBuild = promptBuilder.run(question=q, documents=retrieverResults['documents']) | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| prompt = promptBuild['prompt'] | ||
|
||
|
|
||
| print(f"{prompt=}") | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # rag_pipeline.connect("llm.replies", "answer_builder.replies") | ||
| # rag_pipeline.connect("llm.metadata", "answer_builder.meta") | ||
| # rag_pipeline.connect("retriever", "answer_builder.documents") | ||
| response = llm.run(prompt=prompt, generation_kwargs=None) | ||
|
|
||
| results = answer_builder.run( | ||
| answerBuild = answer_builder.run( | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| query=q, | ||
| replies=response['replies'], | ||
| meta=response['meta'], | ||
| documents=results['documents'], | ||
| documents=retrieverResults['documents'], | ||
| pattern=None, | ||
| reference_pattern=None | ||
| ) | ||
| print("answerBuild", answerBuild) | ||
|
||
|
|
||
| answer = answerBuild['answers'][0] | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| sources= [{ "src": d.meta['src'], "content": d.content, "score": d.score } for d in answer.documents] | ||
rti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| answer = results['answers'][0] | ||
| print("answer", answer) | ||
|
||
|
|
||
| return { | ||
| "answer": answer.data, | ||
| "sources": [{ | ||
| "src": d.meta['src'], | ||
| "content": d.content, | ||
| "score": d.score | ||
| } for d in answer.documents] | ||
| "sources": sources | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
| # {% endfor %} | ||
| # """ | ||
|
|
||
| prompt_template = """ | ||
| prompt_template_en = """ | ||
| <|system|> | ||
| You are a helpful assistant. You answer questions based on the given documents. | ||
| Answer based on the documents only. If the information is not in the documents, | ||
|
|
@@ -25,6 +25,22 @@ | |
| <|assistant|> | ||
| """ | ||
|
|
||
| prompt_template_de = """ | ||
| <|system|> | ||
| Du bist ein hilfreicher Assistent. Du beantwortest Fragen basierend auf den vorliegenden Dokumenten. | ||
| Beantworte basierend auf den Dokumenten nur. Wenn die Information nicht in den Dokumenten ist, | ||
| sage, dass du sie nicht finden kannst. | ||
| <|endoftext|> | ||
| <|user|> | ||
| Dokumente: | ||
| {% for doc in documents %} | ||
|
||
| {{ doc.content }} | ||
| {% endfor %} | ||
| Mit diesen Dokumenten, beantworte die folgende Frage: {{question}} | ||
| <|endoftext|> | ||
| <|assistant|> | ||
| """ | ||
|
|
||
| # prompt_template = """ | ||
| # Given these documents, answer the question. Answer in a full sentence. Give the response only, no explanation. Don't mention the documents. | ||
| # Documents: | ||
|
|
@@ -33,4 +49,7 @@ | |
| # {% endfor %} | ||
| # """ | ||
|
|
||
| prompt_builder = PromptBuilder(template=prompt_template) | ||
| prompt_builder = { | ||
| 'en': PromptBuilder(template=prompt_template_en), | ||
| 'de': PromptBuilder(template=prompt_template_de), | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.