-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (58 loc) · 1.91 KB
/
Copy pathmain.py
File metadata and controls
72 lines (58 loc) · 1.91 KB
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
from elasticsearch import Elasticsearch
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List, Tuple
client = Elasticsearch('https://touche25-rad.webis.de/arguments')
index = "claimrev"
# Types
class Message(BaseModel):
role: str
content: str
class Request(BaseModel):
messages: List[Message]
def reply(messages: List[Message]):
"""
Continues the conversation.
Args:
messages: The conversation so far
Returns:
str: The response text
List: The list of arguments that were used to generate that response, each an object with at least the collection "id"
"""
claim = messages[-1].content
top_result = next(query_elastic(claim))
return top_result["text"], [ top_result ]
def query_elastic(claim, size=1):
"""
Simple function to query the RAD Elasticsearch server.
Args:
claim: The claim to be rebutted
size: The amount of results to retrieve
Returns:
Generator of result objects
"""
# see https://elasticsearch-py.readthedocs.io/en/v8.17.0/api/elasticsearch.html#elasticsearch.client.Elasticsearch.search
response = client.search(index=index, query={
"match": {
"attacks": {
"query": claim
}
}
}, source_excludes=["text_embedding_stella", "supports_embedding_stella", "attacks_embedding_stella"], size=size)
rank = 1
for hit in response["hits"]["hits"]:
result = hit["_source"]
result["key"] = rank
result["id"] = hit["_id"]
result["score"] = hit["_score"]
rank += 1
yield result
# Simple application server -- no need to touch this code if you extend this system
app = FastAPI()
@app.post("/")
async def respond(request: Request):
content, arguments = reply(request.messages)
return {
"content": content,
"arguments": arguments
}