VanessaHochwald commited on
Commit
08eb4c6
·
verified ·
1 Parent(s): ffb3a6b

Upload 4 files

Browse files
Files changed (4) hide show
  1. .DS_Store +0 -0
  2. app.py +54 -166
  3. gender_document.md +448 -0
  4. rag.py +229 -0
.DS_Store ADDED
Binary file (6.15 kB). View file
 
app.py CHANGED
@@ -1,171 +1,59 @@
1
- import streamlit as st
2
- from transformers import pipeline
3
- from sentence_transformers import SentenceTransformer, util
4
- import pdfplumber
5
- import re
6
-
7
- # ---- App Setup ----
8
- st.set_page_config(page_title='Gender Strategy Chatbot', layout='wide', initial_sidebar_state='expanded')
9
- st.title("Chatbot for Gender Strategy Document")
10
-
11
- # ---- Helper Functions ----
12
- def extract_text_from_pdf(pdf_path):
13
- """Extracts text from a PDF file."""
14
- text = ""
15
- with pdfplumber.open(pdf_path) as pdf:
16
- for page in pdf.pages:
17
- text += page.extract_text()
18
- return text
19
-
20
- def preprocess_text(document_text):
21
- """Processes the text, removes hard line breaks, and ensures clean paragraphs."""
22
- # 1. Remove hyphenation and line breaks, but keep the word intact
23
- document_text = re.sub(r'(?<=\S)-\n(?=\S)', '', document_text) # Remove hyphenation and \n
24
- # 2. Merge hard line breaks that occur between two words without hyphenation into a single space
25
- document_text = re.sub(r'(?<=\S)\n(?=\S)', ' ', document_text)
26
- # 3. Remove unnecessary whitespace at the beginning and end of the text
27
- document_text = document_text.strip()
28
- # 4. Optional: Reduce multiple consecutive spaces to a single space
29
- document_text = re.sub(r'\s{2,}', ' ', document_text)
30
- # Return the processed text
31
- standardized_text = document_text
32
- return standardized_text
33
-
34
-
35
- def semantic_search(query, corpus, model):
36
- """Performs semantic search to find the most relevant text in the corpus."""
37
- query_embedding = model.encode(query, convert_to_tensor=True)
38
- corpus_embeddings = model.encode(corpus, convert_to_tensor=True)
39
-
40
- scores = util.pytorch_cos_sim(query_embedding, corpus_embeddings)[0]
41
- best_match_idx = scores.argmax().item()
42
- return corpus[best_match_idx], scores[best_match_idx].item()
43
-
44
- # ---- Load PDF and Extract Text ----
45
- @st.cache_data
46
- def load_pdf_and_prepare_embeddings(pdf_path):
47
- """Loads a PDF, extracts text, standardizes formatting, splits into chunks, and prepares embeddings."""
48
- document_text = extract_text_from_pdf(pdf_path)
49
- standardized_text = preprocess_text(document_text)
50
- chunks = standardized_text.split("\n\n") # Splitting text into chunks by paragraphs
51
- model = SentenceTransformer('all-MiniLM-L6-v2')
52
- return chunks, model
53
 
54
- pdf_path = "giz-2019-en-gender-strategy-web-version-with-bookmarks.pdf"
55
- chunks, embedding_model = load_pdf_and_prepare_embeddings(pdf_path)
56
-
57
- # ---- User Input Section ----
58
- st.sidebar.header("Ask a Question")
59
- query = st.sidebar.text_area("Type your question here:")
60
-
61
- if st.sidebar.button("Submit"):
62
- if query.strip() == "":
63
- st.sidebar.error("Please enter a question.")
64
- else:
65
- with st.spinner("Searching for the best answer..."):
66
- answer, score = semantic_search(query, chunks, embedding_model)
67
- st.write("### Your Question:")
68
- st.write(query)
69
- st.write("### Best Match:")
70
- st.write(answer)
71
- st.write(f"**Relevance Score:** {score:.2f}")
72
-
73
- # ---- Info Section ----
74
- with st.expander("ℹ️ - About this app"):
75
- st.write(
76
- """
77
- This chatbot allows users to ask questions about the Gender Strategy document.
78
- It uses a semantic search model (`all-MiniLM-L6-v2`) to find the most relevant passages from the document.
79
-
80
- - The document is pre-loaded and processed into searchable chunks.
81
- - The model ranks the relevance of the results based on cosine similarity.
82
 
83
- For feedback or improvements, please contact the developer.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  """
85
  )
86
 
87
- # import streamlit as st
88
- # from transformers import pipeline
89
- # from sentence_transformers import SentenceTransformer, util
90
- # import pdfplumber
91
-
92
- # # ---- App Setup ----
93
- # st.set_page_config(page_title='Gender Strategy Chatbot', layout='wide', initial_sidebar_state='expanded')
94
- # st.title("Chatbot for Gender Strategy Document")
95
-
96
- # # ---- Helper Functions ----
97
- # def extract_text_from_pdf(pdf_path):
98
- # """Extracts text from a PDF file."""
99
- # text = ""
100
- # with pdfplumber.open(pdf_path) as pdf:
101
- # for page in pdf.pages:
102
- # text += page.extract_text()
103
- # return text
104
-
105
- # def preprocess_text(document_text):
106
- # """Processes the text, removes hard line breaks, and ensures clean paragraphs."""
107
- # # 1. Remove hyphenation and line breaks, but keep the word intact
108
- # document_text = re.sub(r'(?<=\S)-\n(?=\S)', '', document_text) # Remove hyphenation and \n
109
- # # 2. Merge hard line breaks that occur between two words without hyphenation into a single space
110
- # document_text = re.sub(r'(?<=\S)\n(?=\S)', ' ', document_text)
111
- # # 3. Remove unnecessary whitespace at the beginning and end of the text
112
- # document_text = document_text.strip()
113
- # # 4. Optional: Reduce multiple consecutive spaces to a single space
114
- # document_text = re.sub(r'\s{2,}', ' ', document_text)
115
- # # Return the processed text
116
- # standardized_text = document_text
117
- # return standardized_text
118
-
119
-
120
- # def semantic_search(query, corpus, model):
121
- # """Performs semantic search to find the most relevant text in the corpus."""
122
- # query_embedding = model.encode(query, convert_to_tensor=True)
123
- # corpus_embeddings = model.encode(corpus, convert_to_tensor=True)
124
-
125
- # scores = util.pytorch_cos_sim(query_embedding, corpus_embeddings)[0]
126
- # best_match_idx = scores.argmax().item()
127
- # return corpus[best_match_idx], scores[best_match_idx].item()
128
-
129
- # # ---- Load PDF and Extract Text ----
130
- # @st.cache_data
131
- # def load_pdf_and_prepare_embeddings(pdf_path):
132
- # """Loads a PDF, extracts text, standardizes formatting, splits into chunks, and prepares embeddings."""
133
- # document_text = extract_text_from_pdf(pdf_path)
134
- # standardized_text = preprocess_text(document_text)
135
- # chunks = standardized_text.split("\n\n") # Splitting text into chunks by paragraphs
136
- # model = SentenceTransformer('all-MiniLM-L6-v2')
137
- # return chunks, model
138
-
139
- # pdf_path = "giz-2019-en-gender-strategy-web-version-with-bookmarks.pdf"
140
- # chunks, embedding_model = load_pdf_and_prepare_embeddings(pdf_path)
141
-
142
- # # ---- User Input Section ----
143
- # st.sidebar.header("Ask a Question")
144
- # query = st.sidebar.text_area("Type your question here:")
145
-
146
- # if st.sidebar.button("Submit"):
147
- # if query.strip() == "":
148
- # st.sidebar.error("Please enter a question.")
149
- # else:
150
- # with st.spinner("Searching for the best answer..."):
151
- # answer, score = semantic_search(query, chunks, embedding_model)
152
- # st.write("### Your Question:")
153
- # st.write(query)
154
- # st.write("### Best Match:")
155
- # st.write(answer)
156
- # st.write(f"**Relevance Score:** {score:.2f}")
157
-
158
- # # ---- Info Section ----
159
- # with st.expander("ℹ️ - About this app"):
160
- # st.write(
161
- # """
162
- # This chatbot allows users to ask questions about the Gender Strategy document.
163
- # It uses a semantic search model (`all-MiniLM-L6-v2`) to find the most relevant passages from the document.
164
-
165
- # - The document is pre-loaded and processed into searchable chunks.
166
- # - The model ranks the relevance of the results based on cosine similarity.
167
-
168
- # For feedback or improvements, please contact the developer.
169
- # """
170
- # )
171
-
 
1
+ """This is an example of a simple chatbot that uses the RAG model to answer questions
2
+ about GIZ with Streamlit."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ # Here we import the rag_pipeline function from the rag.py file
7
+ from rag import rag_pipeline
8
+
9
+
10
+ # We use the st.cache decorator to load the RAG pipeline only once and cache it
11
+ @st.cache_resource
12
+ def load_rag_pipeline():
13
+ rag = rag_pipeline()
14
+ return rag
15
+
16
+
17
+ rag = load_rag_pipeline()
18
+
19
+ with st.chat_message(name="ai", avatar=":material/smart_toy:"):
20
+ st.html(
21
+ """Hi, I am an assistant who can answer questions about our organization's gender strategy.
22
+ Please ask me a question.
23
+ <br>
24
+ <b> Note that I cannot memorize our course of our conversation.
25
+ So please always include all the necessary information in your questions and
26
+ be as specific as possible.</b>
27
+ <br>
28
+ <ul>Here are a few examples of possible questions:
29
+ <li> What are the main targets of the gender strategy? </li>
30
+ <li> How does GIZ define “gender” and what understanding is the strategy based on? </li>
31
+ <li> How is the success of the strategy measured and reviewed? </li>
32
+ </ul>
33
  """
34
  )
35
 
36
+ if "messages" not in st.session_state:
37
+ st.session_state.messages = []
38
+
39
+ for message in st.session_state.messages:
40
+ with st.chat_message(name=message["role"], avatar=message["avatar"]):
41
+ st.markdown(message["content"])
42
+
43
+ prompt = st.chat_input("Say something")
44
+ if prompt:
45
+ with st.chat_message(name="user", avatar=":material/person:"):
46
+ st.write(prompt)
47
+ st.session_state.messages.append(
48
+ {"role": "user", "content": prompt, "avatar": ":material/person:"}
49
+ )
50
+ with st.chat_message(name="ai", avatar=":material/smart_toy:"):
51
+ result = rag.run(
52
+ {"prompt_builder": {"query": prompt}, "text_embedder": {"text": prompt}},
53
+ )
54
+ result = result["llm"]["replies"][0]
55
+ result = result.split("Question:")[0]
56
+ st.write(result)
57
+ st.session_state.messages.append(
58
+ {"role": "ai", "content": result, "avatar": ":material/smart_toy:"}
59
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
gender_document.md ADDED
@@ -0,0 +1,448 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # GIZ Gender Strategy
2
+
3
+ ## Gender reloaded:
4
+ Vision needs Attitude –
5
+ Attitude meets Action
6
+
7
+ giz Deutsche Gesellschaft
8
+ für Internationale
9
+ Zusammenarbeit (GIZ) GmbH
10
+ ---
11
+ GIZ Gender Strategy
12
+
13
+ As a federally owned enterprise, GIZ supports the German Government
14
+ in achieving its objectives in the field of international cooperation for
15
+ sustainable development.
16
+
17
+ Published by:
18
+ Deutsche Gesellschaft für
19
+ Internationale Zusammenarbeit (GIZ) GmbH
20
+
21
+ Registered offices
22
+ Bonn and Eschborn, Germany
23
+
24
+ | | |
25
+ |---------------------------|---------------------------|
26
+ | Friedrich-Ebert-Allee 36 + 40 | Dag-Hammarskjöld-Weg 1 - 5 |
27
+ | 53113 Bonn, Germany | 65760 Eschborn, Germany |
28
+ | T +49 228 44 60-0 | T +49 6196 79-0 |
29
+ | F +49 228 4460-17 66 | F +49 6196 79-11 15 |
30
+
31
32
+ I www.giz.de
33
+
34
+ Responsible:
35
+ Dr. Dirk Aßmann, GIZ Gender Ambassador and Director-General, Sectoral Department
36
+
37
+ Author/Editor:
38
+ Dr. Angela Langenkamp, GIZ Gender Commissioner
39
+
40
+ Design/Layout:
41
+ Ira Olaleye, Eschborn
42
+
43
+ GIZ is responsible for the content of this publication.
44
+
45
+ Printing and distribution:
46
+ Name, Ort
47
+
48
+ Printed on 100% recycled paper, certified to FSC standards.
49
+
50
+ Eschborn, January 2019
51
+
52
+ 2
53
+ ---
54
+ # Foreword
55
+
56
+ Dear colleagues,
57
+ commissioning parties and clients,
58
+ partners in Germany and in the field,
59
+
60
+ 'Gender reloaded: Vision needs Attitude – Attitude meets Action' is the motto behind GIZ's
61
+ new Gender Strategy. With it we want to tackle and address current challenges in fostering
62
+ equality of opportunity and rights for all individuals, regardless of their gender, sexual orientation or gender identity. It is about fostering human dignity for all.
63
+
64
+ In May 2016, the GIZ management commissioned a revision of the 2012 Gender Strategy. This
65
+ work was to be based on the findings and recommendations of the corporate strategic evaluation of the 2012 Gender Strategy and its implementation, and on the growing relevance
66
+ of gender equality and women's rights in both the national and the international context, as
67
+ demonstrated by the 2030 Agenda for Sustainable Development and the new European Consensus on Development. They clearly highlight gender equality as a goal in its own right, a
68
+ value, a principle for action and a central objective across sectors and areas of action. In the
69
+ European Consensus on Development it reads 'Gender equality is at the core of the EU values
70
+ and is enshrined in its legal and political framework. It is vital for achieving the SDGs and
71
+ cuts across the whole 2030 Agenda. The EU and its Member States will promote women's
72
+ and girls' rights, gender equality and the empowerment of women and girls and their protection as a priority across all areas of action.'
73
+
74
+ Gender equality is an objective to which we are committed both as a company and as individuals working to shape a future worth living around the world. The GIZ Gender Strategy
75
+ embodies the company's clear position on this, both internally and with regard to the wider
76
+ world. The Gender Strategy is an integral part of our corporate and strategic direction and is
77
+ therefore binding for all members of staff.
78
+
79
+ 'Vision needs Attitude – Attitude meets Action' – this is where you have a part to play. Only
80
+ by working together and contributing each individual's potential and ideas, experience and
81
+ commitment can we combat gender-specific disadvantages and discrimination and make
82
+ gender equality around the world a true-life reality as part of a dignified future.
83
+
84
+ We are prepared and ready – and we look forward to working with you.
85
+
86
+ Dr. Christoph Beier
87
+ Managing Director
88
+
89
+ Dr. Dirk Aßmann
90
+ Director-General, Sectoral Department
91
+
92
+ [Signatures]
93
+ ---
94
+ GIZ Gender Strategy
95
+
96
+ # Table of contents
97
+
98
+ Foreword | 3
99
+ I. Introduction | 5
100
+ Gender equality as a benchmark and quality feature at GIZ | 5
101
+ II. Gender strategy: vision, objectives, strategic elements,
102
+ addressees and scope of application | 7
103
+ a. Objectives | 7
104
+ b. Strategic elements | 8
105
+ c. Addressees and scope of application | 8
106
+ III. Implementation | 9
107
+ a. Organisation | 9
108
+ b. Resources | 9
109
+ c. Monitoring | 10
110
+ Annex | 11
111
+ I GIZ's Gender Architecture | 11
112
+ II ToR for the GIZ Gender Commissioner
113
+ Role: Coordinator and GIZ spokesperson for gender | 14
114
+ III ToR of the gender focal points in the departments and corporate units
115
+ Role: To coordinate implementation of the gender strategy within the
116
+ organisational units and to act as representatives of these organisational units | 15
117
+ IV Glossary | 16
118
+ ---
119
+ | Gender reloaded: Vision needs Attitude – Attitude meets Action
120
+
121
+ # I. Introduction
122
+
123
+ ## Gender equality as a benchmark and quality feature at GIZ
124
+
125
+ Gender equality is one of the key values of our company and of the work we do. It is a prerequisite for and driver of sustainable development and a viable future of our society, both at national and international level. At GIZ, we take a gender-sensitive and wherever needed a gender-differentiated approach and consistent action to eliminate existing gender-based discrimination and to foster equal rights and opportunities for everyone, regardless of their gender, sexual orientation and gender identity. This and our targeted promotion of gender equality are quality features of the Deutsche Gesellschaft für Internationale Zusammenarbeit (GIZ) GmbH.
126
+
127
+ Gender equality is a human right whose realisation is neither a given nor something that happens and will be realised all on its own. It entails a conscious approach to the transformation of gender relations and, above all, it calls for active engagement on the part of us all. Gender equality is enshrined in Article 3 of the Basic Law of the Federal Republic of Germany, to which we feel deeply committed.
128
+
129
+ (1) All persons shall be equal before the law.
130
+
131
+ (2) Men and women have equal rights. The state promotes the actual implementation of equal rights for women and men and take steps to eliminate disadvantages that now exist.
132
+
133
+ (3) No person shall be favoured or disadvantaged because of sex, parentage, ethnicity, language, homeland and origin, faith, or religious or political opinions. No person shall be disfavoured because of disability.
134
+
135
+ Gender equality and the elimination of gender-based discrimination and disadvantages is a core objective and guiding principle of international, European and German (development) policies. By aligning our work, strategies and processes with these objective and principles, and advising and working with our partners and commissioning parties on how to design and implement measures to realise gender equality, we are contributing to a number of national and international agreements, in particular the 2030 Agenda, the European Consensus on Development and the German Sustainable Development Strategy.
136
+ ---
137
+ GIZ Gender Strategy |
138
+
139
+ The promotion of gender equality and the elimination of gender-based disadvantages and discrimination are two strategic pillars of our corporate-policy orientation, which is reflected in our services and in our equal opportunity policy within the company. The GIZ Gender Strategy embodies those two pillars and actively communicates them inside and outside the company. By turning the Gender Policy into action and documenting its implementation we intend to further enhance and consolidate the perception of GIZ as a value-based and responsible company, a reliable partner and an attractive employer. We consider the implementation of the GIZ Internal Plan for Equal Opportunities and the Women's Empowerment Principles on the one hand, and the consistent application of the Safeguards+Gender Management System to new and follow-on projects on the other, as a key foundation for our corporate sustainability, our credibility and our ability to deliver results, both in Germany and in our partner countries.
140
+
141
+ The Safeguards+Gender Management System is applied for all of GIZ's commissioning parties, clients and business areas. It assesses commissions, projects and programmes in their specific context, identifies opportunities to promote gender equality and also potential risks and unintended negative impacts at an early stage, draws up specific measures – in the case of risks and unintended negative impacts known as 'safeguards' – and follows them up in the project cycle.
142
+
143
+ Equal opportunity within the company is both a declaration of what we believe in and a human resources policy directive. We work with other people without any distinction in terms of gender, marital status, skin colour, religion or world view, culture, education, ethnicity, disability, age, sexual orientation, gender identity or nationality.
144
+
145
+ We do not tolerate sexual harassment inside GIZ or through employees and members of our workforce.
146
+
147
+ GIZ's Gender Strategy, with its comprehensive understanding of gender, contributes to our diversity management within the company.
148
+ ---
149
+ | Gender reloaded: Vision needs Attitude – Attitude meets Action
150
+
151
+ ## II. Gender strategy: vision, objectives, strategic elements, addressees and scope of application
152
+
153
+ GIZ's Gender Strategy provides us with a binding framework for the entire company. It nevertheless provides the needed flexibility to design and implement measures in line with the specific mandates and tasks of every organisational unit and position within the company. The gender strategy is based on GIZ's vision, corporate values and guiding principles.
154
+
155
+ > Our vision: We work to shape a future worth living around the world.
156
+
157
+ The objectives and design of specific contributions to the strategy's implementation are guided both by the policy requirements of the German Government and by GIZ's Corporate Strategy.
158
+
159
+ ### a. Objectives
160
+
161
+ **Effectiveness** We enhance the effectiveness and sustainability of our service delivery by taking action consistently to eliminate existing gender-based discrimination and to achieve equal rights and opportunities for everyone, regardless of their gender, sexual orientation and gender identity, both within the company and in connection with our commissions.
162
+
163
+ **Business development** We convince our commissioning parties and clients through our proven gender competence. We secure our share of markets by making effective contributions to the gender equality objectives of international, European and German agreements, such as the 2030 Agenda, in line with our clients' needs. We advise our commissioning parties and clients on how to increase the number and quality of measures that focus on gender equality or that enshrine gender equality as a clearly defined area of action in their objectives.
164
+
165
+ **Skills and alliances** We continuously expand our sector-specific and cross-sectoral gender competence. Through our measures to promote equal opportunities within the company, we secure the future of our company and harness the potentials and opportunities of gender-diverse teams for creative and high-quality performance and service delivery.
166
+
167
+ **Economic efficiency** We meet the gender-related requirements of our commissioning parties and clients by applying our gender knowledge and competence efficiently and by standardising existing approaches and processes to the greatest extent possible.
168
+
169
+ 7
170
+ ---
171
+ GIZ Gender Strategy |
172
+
173
+ ## b. Strategic elements
174
+
175
+ The five complementary strategic elements are of key importance for achieving
176
+ GIZ's Vision and objectives of the GIZ Gender Strategy. They serve as yardsticks of
177
+ our performance:
178
+
179
+ | No. | Element | Description |
180
+ |-----|---------|-------------|
181
+ | 1 | Political will and accountability | Clear positioning of the company and of the manner in which managers commit to realising gender equality and promote and follow up on the implementation of the gender strategy in their area of responsibility. |
182
+ | 2 | Corporate culture | Make the patterns of behaviour, codes of conduct and processes that promote and enhance gender equality within the company visible and represent them inside and outside of GIZ. |
183
+ | 3 | Gender competence | Skills and gender knowledge of the workforce to apply and use the relevant instruments and approaches for actively contributing to gender equality and the elimination of gender-based disadvantages and discrimination. |
184
+ | 4 | Process adjustment | Gender-sensitive and gender-differentiated design of all procedures and instruments at Head Office and in the field structure, especially HR management, commission and quality management as well as results and impact monitoring. |
185
+ | 5 | Equal opportunities within the company | Promotion of potentials, equal rights and opportunities for all employees and members of the workforce, irrespective of their gender, sexual orientation and gender identity. Creation of a balanced gender ratio for the different job categories and assignments within the company. |
186
+
187
+ ## c. Addressees and scope of application
188
+
189
+ GIZ's Gender Strategy is the guiding framework for all managers, employees and
190
+ other members of GIZ's workforce. It therefore applies to all Head Office and field
191
+ staff, national personnel, development workers and integrated experts. They stand
192
+ up for gender equality and in doing so help to bring this strategy to life and translate it into specific actions. Managers act as role models in this context. The
193
+ strategy also serves as a basic source of reference and guidance for our commissioning parties and partners and as a set of binding instructions for our subcontractors.
194
+
195
+ 8
196
+ ---
197
+ | Gender reloaded: Vision needs Attitude – Attitude meets Action
198
+
199
+ ## III. Implementation
200
+
201
+ ### a. Organisation
202
+
203
+ The strategic framework of the gender strategy is binding for all organisational units and everyone who works for GIZ. Taking five strategic elements as the starting point, the strategy is implemented at decentralised level in a manner suited to the mandates, fields and forms of work of the individual organisational units. Part 2 of GIZ's Gender Strategy (for internal use only) provides clear guidelines for its operationalisation.
204
+
205
+ In order to achieve the objectives of the gender strategy, the individual organisational units, including the HR Department for equal opportunity within the company, define, adopt and document their own specific action documents, measures and, where necessary or appropriate, key figures/indicators. They are supported in this by the gender focal points they appoint. The action documents, measures and key figures/indicators are used for steering and annual reporting. For transparency and accountability within GIZ they are available on the GIZ Intranet, in the Document Management System (DMS) and/or on the Integrated Digital Applications (IDA).
206
+
207
+ Managers at all levels are responsible for the implementation of this policy. Key bodies and office holders are named and briefly described in the gender architecture (see Annex I). The special roles of the GIZ Gender Commissioner and the gender focal points at departmental and corporate unit level are shown in Annex II and III.
208
+
209
+ ### b. Resources
210
+
211
+ GIZ's company management provides an annual budget to facilitate company-wide measures to implement the gender strategy. This applies to costs, for instance, of the Gender Competition, the implementation of the Gender Week and company-wide GIZ gender network meetings, the digital gender platform and all costs of the GIZ Gender Commissioner (the coordinator and GIZ spokesperson for gender).
212
+
213
+ To ensure the successful implementation of the Gender Strategy at all levels, managers secure the required human resources, time and financial resources in their respective organisational units. They also support the gender focal points in performing their duties and promote their capacity development so that they can discharge their responsibilities in a professional manner.
214
+
215
+ The Human Resources Department plans, establishes and provides the resources for equal opportunity measures within the company and for the gender focal point in the Human Resources Department. The Equal Opportunity Commissioners are released from their other duties.
216
+
217
+ 9
218
+ ---
219
+ GIZ Gender Strategy |
220
+
221
+ ## c. Monitoring
222
+
223
+ In line with GIZ's internal Guidelines for Operationalisation (part 2) and the specific action documents, measures and key figures/indicators, managers in the departments and corporate units are responsible for implementing, monitoring and reporting on the current implementation status of the gender strategy in their area of responsibility. The implementation of GIZ's Gender Strategy is reflected in the corporate annual objectives. The GIZ Gender Commissioner examines the implementation status of the Gender Strategy and is responsible for the budget provisions by corporate management. In cooperation with the gender focal points of the individual organisational units the Gender Commissioner collates a consolidated company-wide implementation report (including recommendations) and submits it to GIZ's Management Committee and Strategy Committee on an annual basis. The key results are incorporated into the Integrated Company Report and into other reports and declarations (e.g. UN Global Compact, German Sustainability Code).
224
+
225
+ The annual reports from the organisational units are complemented by the results of an online survey among the gender focal points.
226
+
227
+ Monitoring and any recommendations for action on equal opportunities within the company fall under the responsibility of the Human Resources Department. The GIZ Internal Plan for Equal Opportunities is reviewed every two years. The Equal Opportunity commissioners continuously monitor compliance with the relevant legal and HR policy requirements, which are set out in employer/staff council agreements, for example, and call on the Human Resources Department or the Managing Director responsible for the Human Resources Department to ensure they are put into practice. The Management Board reports to the Supervisory Board once a year on the status of equal opportunities and gender equality within GIZ. The key results are incorporated into the report on the implementation of the GIZ Gender Strategy.
228
+
229
+ 10
230
+ ---
231
+ | Gender reloaded: Vision needs Attitude – Attitude meets Action
232
+
233
+ # Annex
234
+
235
+ ## I GIZ's Gender Architecture
236
+
237
+ ### Management Board
238
+ - Puts the gender strategy into effect and sets the corporate-policy orientation for its implementation; the Managing Director responsible for the Human Resources Department is responsible for equal opportunity policies within the company
239
+ - Appoints a Gender Ambassador among the members of the Strategy Committee
240
+
241
+ ### Strategy Committee
242
+ - Advises the Management Board on fundamental corporate-policy issues with regard to gender equality
243
+ - Annual debate concerning gender
244
+
245
+ ### Management Committee
246
+ - Steers and ensures the implementation of the Management Board's corporate-policy instructions on gender in operational business
247
+ - Advises and makes decisions on topic related and procedural issues
248
+
249
+ ### Gender Ambassador
250
+ - Actively promotes GIZ's Gender Strategy and gender equality as such and acts as an interface between specialist and executive management levels
251
+ - Acts as line manager for the GIZ Gender Commissioner and arranges for the Strategy Committee's annual debate on gender
252
+
253
+ ### GIZ Gender Commissioner
254
+ - Coordinator and GIZ spokesperson for gender
255
+ - See Annex II for detailed ToR
256
+
257
+ ### Human Resources Department
258
+ - Draws up strategies on equal opportunities and gender equality within the company
259
+ - Plans and implements measures to promote equal opportunities and gender equality within the company
260
+ - Supports managers in implementing the GIZ Internal Plan for Equal Opportunities
261
+
262
+ 11
263
+ ---
264
+ # GIZ Gender Strategy
265
+
266
+ ## Sectoral division – Gender
267
+
268
+ - Ensures excellence in sectoral expertise on gender as well as the ability to deliver sector-specific services
269
+ - Takes responsibility for setting up and further developing specialised gender know-how and sectoral/methodological knowledge management
270
+ - Promotes the integration of gender into commission management and develops interdisciplinary service packages in cooperation with other departments
271
+
272
+ ## Safeguards+Gender Desk
273
+
274
+ - The Safeguards+Gender Desk of the Internal Customer Services Division (PIC) of the Sectoral Department provides advice on gender-related procedural questions in connection with the Safeguards+Gender Management System
275
+ - Checks the plausibility of preliminary (Safeguards+Gender checklist, possibly gender scan, provisional gender analysis) and in-depth gender analysis
276
+
277
+ ## Gender coordination group
278
+
279
+ - Consists of the Gender Ambassador, the GIZ Gender Commissioner, the gender focal points of the departments and corporate units including the GIZ representative offices in Brussels and Berlin, the Equal Opportunity Commissioner at company level, one representative of the responsible sectoral division and the sector programme
280
+ - Supports and monitors decentralised implementation of the gender strategy within the organisational units
281
+ - Compiles the results achieved in the different organisational units and identifies joint areas of action on that basis
282
+ - Supports the GIZ Gender Commissioner in compiling the report on implementing the gender strategy and documents to be submitted to the Strategy Committee
283
+ - Appoints members for working groups established by the GIZ Gender Commissioner
284
+
285
+ ## Gender network
286
+
287
+ - Consists of all Head Office and field structure gender focal points
288
+ - Communicates the gender strategy inside the company
289
+ - Encourages the discussion of innovative topics, issues and challenges that are important from a corporate policy stance
290
+ - Ensures a supra-regional and cross-sectoral exchange on digital networking formats and at least one Gender Network Meeting each year
291
+ ---
292
+ | Gender reloaded: Vision needs Attitude – Attitude meets Action
293
+
294
+ ### Thematic forum on gender
295
+
296
+ - Consists of planning officers and gender focal points from the Sectoral Department (FMB) and the Sector and Global Programmes Department (GloBe)
297
+ - Is coordinated by the two departmental gender focal points of FMB and GloBe
298
+ - Draws up joint sectoral documents on specific themes
299
+ - Contributes to knowledge management on gender in commission management processes and in sectors
300
+ - Organises joint events with internal and external discussion partners
301
+
302
+ ### Gender focal points
303
+
304
+ - The basis for effective work by the gender officers in the various organisational and work units is a set of custom-tailored ToRs that are adopted by the individual management teams.
305
+ - Gender focal points at departmental and corporate unit level actively support the GIZ Gender Commissioner in performing his/her duties
306
+ - In general, all gender officers:
307
+ - advise and support managers in implementing the gender strategy in their organisational unit or work unit
308
+ - contribute to monitoring and annual reporting and to company-wide gender-specific initiatives and events
309
+ - participate in networks and in formats for sharing information and knowledge management
310
+
311
+ ### Equal Opportunity Commissioner
312
+
313
+ - Promote and monitor the implementation of the GIZ employer/staff council agreement on equal opportunities and compliance with the principles of the German General Equal Treatment Act (AGG) with regard to protection against gender-based discrimination and sexual harassment within the company.
314
+ - The Equal Opportunity Commissioner at corporate level is the employer's contact for all overarching issues related to equal opportunities and gender equality within the company and measures that relate to or affect the company as a whole, a number of units within the company, or cross-departmental workforce groups
315
+
316
+ 13
317
+ ---
318
+ GIZ Gender Strategy |
319
+
320
+ ## II ToR for the GIZ Gender Commissioner
321
+ Role: Coordinator and GIZ spokesperson for gender
322
+
323
+ ### (1) Implementation of the gender strategy throughout the company
324
+ - Coordinating the steps involved in implementing the strategy
325
+ - Elaborating the annual report on implementation of the strategy and coordinating company-wide monitoring
326
+ - Initiating and chairing specialised working groups; steering those working groups or delegating this steering role in consultation with the gender coordination group
327
+ - Coordinating the Gender Week and Gender Competitions
328
+ - Elaborating and coordinating the external image and communication of the Gender Strategy
329
+ - Systematising the progress on implementing the strategy achieved by the various departments, corporate units and other organisational units within the company and communicating this information to managerial level and the gender networks
330
+
331
+ ### (2) Sectoral advice
332
+ - Advisory services to the Gender Ambassador, the Management Committee and the Strategy Committee
333
+ - Delivering advisory services on gender mainstreaming throughout the company
334
+ - Providing impetus for further conceptual development work and the consideration of gender in corporate-strategy documents and processes
335
+ - Positioning the issue of gender within GIZ by providing specialist inputs
336
+ - Ensuring a regular strategic exchange with the Equal Opportunity Commissioner
337
+ - Supporting the gender focal points of the departments and corporate units in networking inside and outside the company
338
+ - Underpinning knowledge management at company level in cooperation with, and with the active support of the Sectoral Department (FMB), the operational departments and the Client Liaison and Business Development Department (AGE)
339
+
340
+ ### (3) Administration
341
+ - Coordinating and supporting the gender coordination group
342
+ - Coordinating and supporting the gender network
343
+ - Producing and following up on the annual plan
344
+ - Accepting responsibility for the pertinent budget
345
+
346
+ 14
347
+ ---
348
+ Gender reloaded: Vision needs Attitude – Attitude meets Action
349
+
350
+ ### III ToR of the gender focal points in the departments and corporate units
351
+
352
+ Role: To coordinate the implementation of the gender strategy within the organisational units and to act as representatives of these organisational units
353
+
354
+ #### (1) Implementation of the gender strategy within the organisational units
355
+ - Facilitating and supporting the implementation of organisational unit-specific action documents and measures, concepts and/or strategies
356
+ - Advising managerial groups on the implementation of the gender strategy
357
+ - Supporting managerial groups in reviewing implementation needs
358
+ - Coordinating and supporting the gender focal points of their respective organisational units with a special focus on the field structure
359
+ - Supporting the organisation of the Gender Week and Gender Competitions and other company-wide initiatives and events on the subject of gender
360
+
361
+ #### (2) Sectoral advice
362
+ - Advising the members of organisational unit-specific gender networks within Germany and abroad on mainstreaming the institutionalisation of gender
363
+ - Securing the flow of information between the field structure and Head Office and among the various Head Office organisational units
364
+ - Facilitating identification and placing of gender focal points or experts and communicating specialist expertise
365
+ - Where applicable, advising on the integration of gender aspects into the terms of reference for appraisal missions, etc.
366
+ - Attending ZUK (approval of the draft strategy) and ZAK (approval of the offer design) meetings within the pertinent department
367
+ - Identifying issues and putting these on the agenda
368
+
369
+ #### (3) Implementation of the gender strategy throughout the company
370
+ - Representing the respective organisational unit within the gender coordination group and attending meetings regularly
371
+ - Representing the respective organisational unit in consultation and coordination processes
372
+ - Supporting the GIZ Gender Commissioner in her/his monitoring work
373
+
374
+ 15
375
+ ---
376
+ GIZ Gender Strategy |
377
+
378
+ ## IV Glossary
379
+
380
+ | Term | Definition |
381
+ |------|------------|
382
+ | Diversity, diversity management | Diversity considers the following aspects in particular: gender, sexual orientation, ethnic and cultural background, religion and beliefs, disability, and age. Diversity management as a tool of modern corporate management has the objective of making constructive and gainful use of diversity within the workforce. Diversity management has also taken on a compliance dimension with the enactment of a range of anti-discrimination law, such as the German General Act on Equal Treatment (AGG). |
383
+ | Empowerment | Empowerment means giving an individual power or transferring responsibility to them. The term is often used in the context of economic empowerment or women's empowerment. Empowerment includes strategies and measures intended to increase the degree of autonomy and self-determination that individuals and societies have over their lives and to enable them to (re-)assert their interests independently, responsibly and autonomously. In line with the 1995 Beijing Declaration and Platform for Action, the empowerment approach, among other things, particularly aims to use awareness-raising at all levels of society to strengthen women as legal entities and to broaden their agency. |
384
+ | Gender | Gender refers to an individual's social gender as opposed to their sex, which is biologically determined. It includes socially constructed gender roles and relationships, perceptions and expectations. These factors are contextual, dynamic and open to change. They are reflected in such areas as social standards, legislation, traditions, religion and so on. |
385
+ | Gender competence | Gender competence includes the ability to reflect critically on gender relationships and social gender attributions (e.g. gender stereotypes) by using knowledge of how such attributions arise and their social impact. Gender competence also includes the ability to use and apply this knowledge, for example in project planning and implementation and in the design of new approaches and strategies. |
386
+
387
+ 16
388
+ ---
389
+ | Gender reloaded: Vision needs Attitude – Attitude meets Action
390
+
391
+ ## Gender-sensitive design of all procedures, gender-sensitive approach
392
+
393
+ A gender-sensitive approach takes account of gender-specific inequalities and gender discrimination and of the diverse interests, needs and potentials of different genders within a specific context. It recognises and identifies existing gender-specific differences, problems and inequalities and integrates them into strategies and measures. The objective is to ensure that no unintended negative impact results from these strategies and measures and that individuals are able to participate in and benefit from (development cooperation) measures irrespective of their gender.
394
+
395
+ ## Gender identity
396
+
397
+ Gender identity is the gender with which an individual identifies. Gender diversity is based on feelings of belonging to a particular gender and gender identity, for example as a woman, a man, transgender or intersex or other local and indigenous self-identities, such as Hijra in India and Pakistan.
398
+
399
+ ## Equal opportunities within the company
400
+
401
+ Equal opportunities within the company aim to secure equality at work for women and men. Within GIZ, such equality is based primarily on the company/staff council agreement on gender equality and on the gender strategy.
402
+
403
+ ![A logo with the text "GLEICHSTELLUNG IN DER GIZ" and an image of three overlapping silhouettes in red, white, and blue.]
404
+
405
+ ## Safeguards+Gender
406
+
407
+ GIZ's binding Safeguards+Gender Management System is there to check and ensure that projects and programmes systematically identify potential for promoting gender equality and women's rights as well as unintended negative impacts or potential external risks with regard to gender equality at an early stage, and actively address them during the entire project cycle.
408
+
409
+ ![A circular logo with the text "SAFEGUARDS+GENDER" surrounded by colorful segments.]
410
+
411
+ 17
412
+ ---
413
+ GIZ Gender Strategy |
414
+
415
+ | Term | Definition |
416
+ |------|------------|
417
+ | Sexual harassment | Sexual harassment is a form of gender-based violence encompassing acts of unwanted physical, verbal or non-verbal conduct of a sexual nature, which have a purpose and/or effect of violating the victim's dignity and creating an intimidating, hostile, degrading, humiliating or offensive environment. Individuals of any gender may be both the perpetrator and the target of sexual harassment. The latter includes among others: unwanted physical proximity, touching or assault of a sexual nature; gestures and other forms of non-verbal communication with a sexual nature; sexually-oriented comments on individuals and/or their body, behaviour, sex life or sexual identity; sexually demeaning language and comments designed to undermine dignity, such as jokes with sexual content; requests to carry out sexual acts; sharing or displaying pornographic or sexist images and stalking with a sexual basis. Sexual harassment is particularly severe when a person's hierarchical dependence is exploited in a training context or at the workplace and when personal or professional disadvantages are threatened or advantages are promised. |
418
+ | Sexual orientation | An individual's sexual orientation is the gender to which a person feels drawn emotionally, physically and/or sexually. Distinctions are made between lesbian, gay, heterosexual, bisexual and asexual orientations. The abbreviation LGBTIQ stands for lesbian, gay, bisexual, transgender, intersex & queer. |
419
+ | Transformation of gender relationships, gender-transformative approach | Gender-transformative approaches aim to change gender-specific role attributions, unequal power relationships, structures and social norms, and rules that lead to gender-specific disadvantages, discrimination and/or marginalisation so as to achieve (greater) gender justice. A gender-transformative approach therefore not only focuses on the manifestations and symptoms of gender inequality (such as lack of access) but also tackles its root causes, such as sociocultural norms, discriminatory legal provisions and social systems. |
420
+
421
+ 18
422
+ ---
423
+ | Gender reloaded: Vision needs Attitude – Attitude meets Action
424
+
425
+ | Wide-ranging understanding | Gender diversity, gender inclusivity, an inclusive under- |
426
+ | of gender; gender diversity | standing of gender, and a non-binary understanding of |
427
+ | | gender all mean inclusion of all genders, not just male |
428
+ | | and female. However, in most cases a binary understand- |
429
+ | | ing of gender (male and female) forms the basis for leg- |
430
+ | | islation and dominant social orders. Self-determination of |
431
+ | | gender identity is a basic human right under internation- |
432
+ | | al law. |
433
+
434
+ 19
435
+ ---
436
+ Deutsche Gesellschaft für
437
+ Internationale Zusammenarbeit (GIZ) GmbH
438
+
439
+ Registered offices
440
+ Bonn and Eschborn, Germany
441
+
442
+ Friedrich-Ebert-Allee 36 + 40 Dag-Hammarskjöld-Weg 1 - 5
443
+ 53113 Bonn, Germany 65760 Eschborn, Germany
444
+ T +49 228 44 60-0 T +49 61 96 79-0
445
+ F +49 228 44 60-17 66 F +49 61 96 79-11 15
446
+
447
448
+ I www.giz.de
rag.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ "This file contains the implementation of the RAG pipeline."
2
+
3
+ from pathlib import Path
4
+
5
+ from haystack import Pipeline
6
+ from haystack.components.builders import PromptBuilder
7
+ from haystack.components.converters import MarkdownToDocument
8
+ from haystack.components.embedders import (
9
+ SentenceTransformersDocumentEmbedder,
10
+ SentenceTransformersTextEmbedder,
11
+ )
12
+ from haystack.components.generators import HuggingFaceAPIGenerator
13
+ from haystack.components.preprocessors import DocumentCleaner, DocumentSplitter
14
+ from haystack.components.retrievers import InMemoryEmbeddingRetriever
15
+ from haystack.components.writers import DocumentWriter
16
+ from haystack.document_stores.in_memory import InMemoryDocumentStore
17
+ from haystack.utils import Secret
18
+
19
+ # Define the paths to the document and the model for embedding the documents and the user query
20
+ DOCUMENT_PATH = Path("gender_document.md")
21
+ EMBEDDING_MODEL = "all-MiniLM-L6-v2"
22
+
23
+
24
+ def process_document(document_store: InMemoryDocumentStore) -> Pipeline:
25
+ """This function processes the document and stores it in the document store.
26
+ It contains of the following components:
27
+ - MarkdownToDocument: Converts the markdown file to a document (https://docs.haystack.deepset.ai/docs/markdowntodocument)
28
+ - DocumentCleaner: Cleans the document (https://docs.haystack.deepset.ai/docs/documentcleaner)
29
+ - DocumentSplitter: Splits the document into chunks (https://docs.haystack.deepset.ai/docs/documentsplitter)
30
+ - DocumentWriter: Writes the document to the document store (https://docs.haystack.deepset.ai/docs/documentwriter)
31
+ - SentenceTransformersDocumentEmbedder: Embeds the documents, more precisely the chunks (https://docs.haystack.deepset.ai/docs/sentencetransformersdocumentembedder)
32
+
33
+
34
+ Parameters
35
+ ----------
36
+ document_store : InMemoryDocumentStore
37
+ The document store where the processed document should be stored.
38
+
39
+ Returns
40
+ -------
41
+ Pipeline
42
+ The pipeline containing the components to parse, clean, split, embed and write the document to the document store.
43
+ To run the pipeline, you can use the `pipeline.run()` method. If a component needs input or arguments, you can pass them as a dictionary to the `run()` method.
44
+ For example: `pipeline.run({"converter": {"sources": [DOCUMENT_PATH]}})`.
45
+ """
46
+
47
+ # initialize the pipeline
48
+ pipeline = Pipeline()
49
+
50
+ # add the components to the pipeline. If you want to add more components, you can do it here.
51
+ # If you want to the settings of the components, you can do it here.
52
+ # MarkdownToDocument
53
+ pipeline.add_component("converter", MarkdownToDocument())
54
+
55
+ # DocumentCleaner
56
+ pipeline.add_component("cleaner", DocumentCleaner())
57
+
58
+ # DocumentSplitter
59
+ pipeline.add_component(
60
+ "splitter",
61
+ DocumentSplitter(
62
+ split_by="word", split_length=300, respect_sentence_boundary=True
63
+ ),
64
+ )
65
+
66
+ # DocumentWriter
67
+ pipeline.add_component("writer", DocumentWriter(document_store=document_store))
68
+
69
+ # SentenceTransformersDocumentEmbedder
70
+ pipeline.add_component(
71
+ "embedder",
72
+ SentenceTransformersDocumentEmbedder(
73
+ EMBEDDING_MODEL,
74
+ ),
75
+ )
76
+
77
+ # connect the components
78
+ pipeline.connect("converter", "cleaner")
79
+ pipeline.connect("cleaner", "splitter")
80
+ pipeline.connect("splitter", "embedder")
81
+ pipeline.connect("embedder", "writer")
82
+ return pipeline
83
+
84
+
85
+ def load_document_store(document_store_settings: dict) -> InMemoryDocumentStore:
86
+ """This function loads the document store with the given settings.
87
+
88
+ Parameters
89
+ ----------
90
+ document_store_settings : dict
91
+ The settings for the document store. The settings are passed as a dictionary.
92
+ You can find the available settings here: https://docs.haystack.deepset.ai/docs/inmemorydocumentstore
93
+
94
+ Returns
95
+ -------
96
+ InMemoryDocumentStore
97
+ _description_
98
+ """
99
+ document_store = InMemoryDocumentStore(**document_store_settings)
100
+ return document_store
101
+
102
+
103
+ def get_query_pipeline(
104
+ document_store: InMemoryDocumentStore, generator: HuggingFaceAPIGenerator
105
+ ) -> Pipeline:
106
+ """
107
+ This function creates a query pipeline that contains the following components:
108
+ - SentenceTransformersTextEmbedder: Embeds the user query (https://docs.haystack.deepset.ai/docs/sentencetransformerstextembedder)
109
+ - InMemoryEmbeddingRetriever: Retrieves the most similar documents to the user query (https://docs.haystack.deepset.ai/docs/inmemoryembeddingretriever)
110
+ - PromptBuilder: Builds the prompt for the generator (https://docs.haystack.deepset.ai/docs/promptbuilder)
111
+ - HuggingFaceAPIGenerator: Generates the answer to the user query (https://docs.haystack.deepset.ai/docs/huggingfaceapigenerator)
112
+
113
+ Parameters
114
+ ----------
115
+ document_store : InMemoryDocumentStore
116
+ The document store where the documents are stored.
117
+ llm_provider : HuggingFaceAPIGenerator
118
+ The llm_provider that generates the answer to the user query.
119
+
120
+ Returns
121
+ -------
122
+ Pipeline
123
+ The query pipeline containing the components to embed the user query, retrieve the most similar documents, build the prompt and generate the answer.
124
+ """
125
+
126
+ # initialize the query pipeline
127
+ query_pipeline = Pipeline()
128
+
129
+ # add the components to the query pipeline
130
+ # SentenceTransformersTextEmbedder
131
+ query_pipeline.add_component(
132
+ "text_embedder", SentenceTransformersTextEmbedder(EMBEDDING_MODEL)
133
+ )
134
+
135
+ # InMemoryEmbeddingRetriever
136
+ query_pipeline.add_component(
137
+ "retriever", InMemoryEmbeddingRetriever(document_store=document_store, top_k=10)
138
+ )
139
+
140
+ # template for the PromptBuilder
141
+ template = """
142
+ Given the following information, answer the question. If the information is insufficient, answer with "Answer is not possible". Please do not provide any additional information and ask clarifying questions.
143
+
144
+ Context:
145
+ {% for document in documents %}
146
+ {{ document.content }}
147
+ {% endfor %}
148
+
149
+ Question: {{ query }}?
150
+ """
151
+
152
+ # PromptBuilder
153
+ query_pipeline.add_component("prompt_builder", PromptBuilder(template=template))
154
+
155
+ # HuggingFaceAPIGenerator
156
+ query_pipeline.add_component("llm", generator)
157
+
158
+ # connect the components
159
+ query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
160
+ query_pipeline.connect("retriever", "prompt_builder.documents")
161
+ query_pipeline.connect("prompt_builder", "llm")
162
+ return query_pipeline
163
+
164
+
165
+ def init_generator() -> HuggingFaceAPIGenerator:
166
+ """This function initializes the HuggingFaceAPIGenerator with the given settings.
167
+ You can find the available models here: https://huggingface.co/models?inference=warm&pipeline_tag=text-generation&sort=trending
168
+ Please note that you need to provide a valid token to use the HuggingFaceAPIGenerator.
169
+ For testing purposes, you can hardcode the token in the script.
170
+ For deployment on Hugging Face Spaces, please safe the token as a secret (Settings -> Secrets) and load it with `Secret.from_env_var("your_token_name")`.
171
+
172
+ Returns
173
+ -------
174
+ HuggingFaceAPIGenerator
175
+ _description_
176
+ """
177
+
178
+ # initialize the HuggingFaceAPIGenerator
179
+ llm_provider = HuggingFaceAPIGenerator(
180
+ api_type="serverless_inference_api",
181
+ api_params={"model": "HuggingFaceH4/zephyr-7b-beta", "stop": ["Question"]},
182
+ token=Secret.from_token(""),
183
+ )
184
+ return llm_provider
185
+
186
+
187
+ def rag_pipeline() -> Pipeline:
188
+ """This function wraps the whole RAG pipeline.
189
+ It loads the document store, processes the document, initializes the generator and
190
+ creates the query pipeline.
191
+
192
+ Returns
193
+ -------
194
+ Pipeline
195
+ The RAG pipeline containing the components to process the document and generate
196
+ the answer to the user query. It is enough to import and load this function for the chat application.
197
+ You can run the pipeline with the `pipeline.run()` method.
198
+ If a component needs input or arguments, you can pass them as a dictionary to the `run()` method.
199
+ For example:
200
+ result = rag.run(
201
+ {"prompt_builder": {"query": prompt}, "text_embedder": {"text": prompt}},
202
+ )
203
+ For debugging purposes, you can include the outputs for example from the retriever
204
+ result = rag.run(
205
+ {"prompt_builder": {"query": prompt}, "text_embedder": {"text": prompt}},
206
+ include_outputs_from=["retriever", "llm"],
207
+ )
208
+ """
209
+ # define document_store_settings
210
+ document_store_settings = {"embedding_similarity_function": "cosine"}
211
+
212
+ # load the document store
213
+ document_store = load_document_store(document_store_settings)
214
+
215
+ # process the document and write it to the document store
216
+ document_pipeline = process_document(document_store=document_store)
217
+
218
+ # run the document pipeline
219
+ document_pipeline.run({"converter": {"sources": [DOCUMENT_PATH]}})
220
+
221
+ # initialize the generator
222
+ llm_provider = init_generator()
223
+
224
+ # create the query pipeline
225
+ query_pipeline = get_query_pipeline(
226
+ document_store=document_store, generator=llm_provider
227
+ )
228
+
229
+ return query_pipeline