Skip to content

Commit

Permalink
Add language feature -> English
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels Ringler committed Sep 27, 2024
1 parent c539d8b commit abc25c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
28 changes: 20 additions & 8 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
check_prompt,
check_summary_prompt,
detect_language,
check_content
check_content,
invalid_input_response,
english_response
)

run_id = uuid4()
Expand All @@ -53,6 +55,7 @@ async def completion(
model: OpenAiModel = OpenAiModel.gpt4mini,
honest: bool = True,
raw_output: bool = False,
output_language: str = 'German'
):
"""
Completion endpoint for text generation.
Expand All @@ -72,15 +75,14 @@ async def completion(
[{"role": "system", "content": check_content}]]
tasks = [call_openai_lin(prompt=prompt, messages=message, client=async_client, model=model) for message in messages]
resp = await asyncio.gather(*tasks)
language = resp[0].choices[0].message.content
input_language = resp[0].choices[0].message.content
output_language = json.loads(input_language)['language']
content = resp[1].choices[0].message.content
# Print the response
print(f'{language}, {content}')
print(f'{input_language}, {content}')
if not json.loads(content)['content'] == "ok":
error_message = "Ihr Text kann von dieser Demo leider nicht verarbeitet werden. Das kann verschiedene Gründe haben, z.B. Textqualität oder -inhalt. Bitte versuchen Sie es mit einem anderen Text oder Link."
print("MALICIOUS CONTENT")
return StreamingResponse(
handle_stream(error_message,
handle_stream(invalid_input_response,
all_json=~raw_output,
))

Expand All @@ -89,6 +91,9 @@ async def completion(
else:
system_prompt = system_prompt_malicious

if output_language == 'English':
system_prompt += english_response

messages = [{"role": "system", "content": system_prompt}]

logging.debug(request)
Expand All @@ -103,11 +108,18 @@ async def completion(

@app.post("/check", response_model=CheckResponse)
async def check_article_against_source(
request: CheckRequest, model: OpenAiModel = OpenAiModel.gpt4mini
request: CheckRequest, model: OpenAiModel = OpenAiModel.gpt4mini, output_language = "German"
):
"""
The endpoint compares a given article chunk against a source using an AI model to determine its validity.
"""
# Detect language
messages = [{"role": "system", "content": detect_language}]
resp = call_openai_lin(prompt=request.source, messages=messages, client=client, model=model)
input_language = resp.choices[0].message.content
output_language = json.loads(input_language)['language']
system_prompt = check_prompt if output_language == "German" else check_prompt + english_response

fc = Auditor(request.source, request.chunk)
logging.info( # f'\n\nChecking against each PARAGRAPH that contains similar sentences\n\n'
f"Input:\n{fc.input}\n\n" f"{len(fc.similar_para_id)} similar paragraph(s)\n"
Expand All @@ -117,7 +129,7 @@ async def check_article_against_source(
answers = []

similar_paras = '\n\n'.join([fc.paragraphs[para_id] for para_id in fc.similar_para_id])
messages = [{"role": "system", "content": check_prompt}]
messages = [{"role": "system", "content": system_prompt}]

prompt = "Satz:\n" f"{fc.input}\n\n" "Text:\n" f"{similar_paras}"

Expand Down
10 changes: 10 additions & 0 deletions src/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,14 @@
Beispielantworten:
{"content": "ok"}
{"content": "invalid"}
"""

invalid_input_response = """
Ihr Text kann von dieser Demo leider nicht verarbeitet werden. Das kann verschiedene Gründe haben, z.B. Textqualität oder -inhalt. Bitte versuchen Sie es mit einem anderen Text oder Link.
Unfortunately, your text can't be processed by this demo. There may be various reasons for this, e.g. text quality or content. Please try another text or link.
"""

english_response = """
Additionally, you must only answer and communicate in English, regardless of the language used by system prompt.
"""

0 comments on commit abc25c7

Please sign in to comment.