-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (93 loc) · 4.98 KB
/
main.py
File metadata and controls
129 lines (93 loc) · 4.98 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from fastapi import FastAPI
import uvicorn
#from openai import OpenAI
#from groq import Groq
from mistralai.client import Mistral
from exa_py import Exa
import os
from dotenv import load_dotenv
app = FastAPI()
load_dotenv()
@app.get("/")
def hello():
return {"headline": "Hello, dear developer!", "text_info": "I'm Tim Seufert, a 16 y.o. high school developer and hobbyist with a strong focus on developing well designed, user friendly AI solutions. I'm a student working on personal projects to enhance my software development skills. I've built the FakifyAPI a few months after finalizing my initial Fakify (fake news detector) project. I wanted to create a way for users of the internet to analyze and validate information from sources like articles or blog posts in a secure and reliable way (through retrieval augmented generation with the Mistral and Exa API's). Today I'm proudly presenting the FakifyAPI. This interface should allow users to embed my Fakify service into their own applications and services. All in all my project aims for an easy use of the Fakify project and making the internet a little bit safer. I hope you will enjoy using this API. See you on the internet :) - Tim Seufert", "api_status": "API is operational", "documentation": "/docs and https://timhongphuc-fakifyapi.apidocumentation.com", "about_the_project": "https:github.com/Timhongphuc/FakifyAPI"}
@app.get("/app")
def analysis(url: str):
#def analysis():
#input_text = "https://www.example.com" # Test input.
input_text = url
load_dotenv()
#--------------------------------------------------------------------------
# EXA API (Content Endpoint)
load_dotenv()
if input_text:
exa = Exa(api_key = os.environ.get("EXA_API_KEY"))
response = exa.get_contents(
urls=[input_text],
text= True
)
article_content = response.results[0].text
print(article_content)
#--------------------------------------------------------------------------
# #MISTRAL API (Endpoint No1) Search query for Exa API
load_dotenv()
if input_text:
client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY"))
inputs = [
{"role": "user", "content": f"Generate ONE search query: {article_content}",}
]
response = client.beta.conversations.start(
agent_id= os.environ.get("AGENT_ID"), #System prompt is included in the Mistral API Dashboard
inputs=inputs,
)
# Extract the clean markdown content from the response
search = response.outputs[0].content
print(search)
print("Received search query!")
#--------------------------------------------------------------------------
# EXA API (Search Endpoint)
exa = Exa(api_key = os.environ.get("EXA_API_KEY"))
result = exa.search_and_contents(
search,
category = "news",
livecrawl = "fallback",
num_results=4,
summary = {
"query": "Your task is to create a brief AI summary of the Webpage. Max. 20 Words"
},
text = True,
type = "auto"
)
search_results = result.results[3].text #Take the first 4 Search results (in index 3)
print(search_results)
print("Results fetched!")
#--------------------------------------------------------------------------
# Mistral API (API Endpoint No2) AI Summary of RAG analysis
load_dotenv()
with Mistral(
api_key=os.environ.get("MISTRAL_API_KEY"),
) as mistral:
res = mistral.chat.complete(model="mistral-small-latest", messages=[
{
"content": f"Please provide me with an comprehensive analysis. These are the sources you can use to fulfill your task: The content of the News article you HAVE to check: {article_content}, similar search results to the topic (to verify credibility). Take a deep look into the sources: {search_results}. These sources are really important. If there are other Websites that provide the same information as given in the article, rate the article as real or likely real. If the topic in the article appears in other sources and the source is trustworthy change the rating accordingly.",
"role": "user"
},
{
"content": os.environ.get("SYSTEM_PROMPT"),
"role": "system",
#"response_format": "json_object"
},
], stream=False)
# Handle response
results_final = res.choices[0].message.content
print(results_final)
print("Finished analysis!")
#----------------------------------------------------------------
return {"results": f"{results_final}"}
@app.get("/trustcheck")
def trust():
return{"TrustCheck": "Test Trust!!!"}
@app.get("/stats")
def stats():
return{"Hello": "Hello World!"}
uvicorn.run(app)