import palang
from opensearchpy import OpenSearch

JsonList = palang.import_model("hello::JsonList")
YamlList = palang.import_model("hello::YamlList")

translate = palang.import_program("hello::translate").target_profile("llama3")

client = OpenSearch(
    hosts=[{'host': '10.249.144.176', 'port': 9200}],
    http_auth=('opensearch-client_5', '5Hdpni5roqhMcDglBlGDpJUgarw0UM6E'),  # <-- Replace with your credentials
    use_ssl=False                  # <-- Set to True if using SSL
)

def query_opensearch_for_references(text, index="dictionary-references", size=5):
    """
    Query OpenSearch for dictionary references related to the input text.
    Returns a list of reference strings.
    """
    query_body = {
        "query": {
            "match": {
                "text": text  # <-- Adjust if your index uses a different field
            }
        },
        "size": size
    }
    response = client.search(index=index, body=query_body)
    references = []
    for hit in response['hits']['hits']:
        # Adjust 'reference' to the actual field in your documents
        ref = hit['_source'].get('reference')
        if ref:
            references.append(ref)
    return references

def translate_with_references(text, target_language="French"):
    references = query_opensearch_for_references(text)
    return translate(text, references)

def main():
    text_to_translate = "Hello world"
    translation = translate_with_references(text_to_translate, "French")
    print(f"Translation with references: {translation}")

if __name__ == "__main__":
    main()
