-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
147 lines (116 loc) · 4.77 KB
/
main.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.exceptions import RequestValidationError
from fastapi.responses import PlainTextResponse
from pydantic import BaseModel
from typing import List
import logging
from core.azure_functions import AzureOpenAIFunctions
import config
import functions.argocd as argocd
import functions.web_browsing as browser
import functions.weather as weather
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
logger.propagate = True
# -- The message schema for the assistant
class Message(BaseModel):
role: str
content: str
# -- The conversation schema for the assistant
class Conversation(BaseModel):
conversation: List[Message]
# -- Initialize the FastAPI app
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# -- Exception handler for the FastAPI app
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(exc):
return PlainTextResponse(str(exc), status_code=400)
system_prompt = """You are an AI assistant with access to websearch, Argocd, and weather functions.
The websearch function empowers you for real-time web search and information retrieval, particularly for current and
relevant data from the internet in response to user queries, especially when such information is beyond your training
data or when up-to-date information is essential. Always include the source URL for information fetched from the web.
The Argocd function facilitates real-time interaction with the Argocd instance, enabling you to fetch information about
applications deployed in the cluster.
The weather function provides real-time capabilities to offer current weather updates.
All your responses should be in a human-readable format.
"""
# Initialize the assistant (GPT Model) with the functions
assistant = AzureOpenAIFunctions(
azure_openai_endpoint=config.azure_openai_endpoint,
azure_openai_key_key=config.azure_openai_key_key,
azure_api_version=config.azure_api_version,
model=config.azure_openai_deployment_name,
functions=[
argocd.get_available_applications,
argocd.get_application_status,
weather.get_weather,
browser.text_search,
browser.news_search,
browser.images_search,
browser.videos_search,
browser.maps_search,
browser.webpage_scraper
]
)
# -- FastAPI endpoints
@app.post("/assistant/{conversation_id}")
async def endpoint(conversation_id: str, conversation: Conversation):
system_message = Message(role='system', content=system_prompt)
conversation.conversation.insert(0, system_message)
conversation_dict = [message.model_dump() for message in conversation.conversation]
logger.debug(f"Conversation: {conversation_dict}")
response = assistant.ask(conversation_dict)
logger.debug(f"Reply: {response.choices[0].message.content}")
return {"id": conversation_id, "reply": response.choices[0].message.content}
# -- Test the assistant. This is not part of the FastAPI app, only for demonstration purposes.
if __name__ == "__main__":
prompt = "Is Sam Altman fired from OpenAI?"
response = assistant.ask([{'role': 'user', 'content': f'{prompt}'}])
# ANSI escape sequences for colors
RED = '\033[91m'
GREEN = '\033[92m'
ENDC = '\033[0m' # Resets the color to default
print(f"{RED}\nQuery: {prompt} {ENDC}\n")
print(f"{GREEN}Reply: {response.choices[0].message.content}{ENDC}")
# Features:
# -- Web search
# -- Video search
# -- Location search
# -- Image search
# -- Summarize article from URL
# -- Weather information
# -- Integration with ArgoCD application through rest API.
# === Questions ===
# ---- General search to recent events ----
# -- Is Sam Altman fired from OpenAI?
# -- What happened to HSBC bank in UK?
# -- What happened to WeWork?
# ---- About a person ----
# -- Who is Frank Gotthard?
# ---- Video search ----
# -- Provide video tutorial for Excel pivot table.
# -- Provide video tutorial for Python pandas.
# -- Show me a video about how to make a cake.
# ---- Location search ----
# -- Suggestions for the top 3 Italian restaurant in Munich.
# ---- Image search ----
# -- Provide puppies images.
# -- Provide 10 images of cats.
# -- Show me pictures of the Eiffel Tower.
# -- Show me pictures of the Eiffel Tower at night.
# ---- Weather ----
# -- What is the weather in Berlin today?
# -- Is there any possibility of rain in Berlin today?
# ---- Summarize from URL ----
# -- Summarize the article in 3 sentences https://www.bbc.com/news/world-us-canada-67482231
# ---- External application ----
# -- How many argocd applications are available?
# -- How many argocd applications are available? And what are their status?