forked from StarsRail/Cocai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
236 lines (209 loc) · 8.82 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python
import logging
import os
import chainlit as cl
from llama_index.agent.openai import OpenAIAgent
from llama_index.core import Settings
from llama_index.core.agent import AgentRunner
from llama_index.core.callbacks import CallbackManager, LlamaDebugHandler
from llama_index.core.memory import ChatMemoryBuffer
from llama_index.core.storage.chat_store import SimpleChatStore
from llama_index.core.tools import FunctionTool
from llama_index.embeddings.ollama import OllamaEmbedding
from llama_index.tools.tavily_research import TavilyToolSpec
from openinference.instrumentation.llama_index import LlamaIndexInstrumentor
from tools import (
ToolForConsultingTheModule,
ToolForSuggestingChoices,
illustrate_a_scene,
roll_a_dice,
roll_a_skill,
tool_for_creating_character,
)
from utils import set_up_data_layer
logger = logging.getLogger(__name__)
# This object holds all chat histories that occur throughout the current LlamaIndex process.
# This does not mean all chat histories/chat sessions that happened across boot-ups;
# those are handled by the data persistence layer in Chainlit.
# That said, we would still like to have a global variable for this, so that it can be shared across all chat
# memories / chat sessions; otherwise, the default behavior is initializing one chat store per chat session.
chat_store = SimpleChatStore()
set_up_data_layer()
try:
# "Phoenix can display in real time the traces automatically collected from your LlamaIndex application."
# https://docs.llamaindex.ai/en/stable/module_guides/observability/observability.html
from phoenix.otel import register
tracer_provider = register()
LlamaIndexInstrumentor().instrument(tracer_provider=tracer_provider)
except Exception as e:
logger.warn(f"Failed to register Phoenix OpenTelemetry instrumentation: {e}")
@cl.password_auth_callback
def auth_callback(username: str, password: str):
# Fetch the user matching username from your database
# and compare the hashed password with the value stored in the database
if (username, password) == ("admin", "admin"):
return cl.User(identifier="admin", metadata={"role": "admin"})
else:
return None
def create_callback_manager() -> CallbackManager:
# Phoenix can display in real time the traces automatically collected from your LlamaIndex application.
# The one-click way is as follows:
# ```
# llama_index.core.set_global_handler("arize_phoenix")
# from llama_index.callbacks.arize_phoenix import (
# arize_phoenix_callback_handler,
# )
# ```
# But I prefer to do it manually, so that I can put all callback handlers in one place.
debug_logger = logging.getLogger("debug")
debug_logger.setLevel(logging.DEBUG)
callback_handlers = [
LlamaDebugHandler(logger=debug_logger),
]
callback_handlers.append(cl.LlamaIndexCallbackHandler())
return CallbackManager(callback_handlers)
def set_up_llama_index(max_action_steps: int = 5):
"""
One-time setup code for shared objects across all AgentRunners.
"""
# Needed for "Retrieved the following sources" to show up on Chainlit.
Settings.callback_manager = create_callback_manager()
# ============= Beginning of the code block for wiring on to models. =============
# At least when Chainlit is involved, LLM initializations must happen upon the `@cl.on_chat_start` event,
# not in the global scope.
# Otherwise, it messes up with Arize Phoenix: LLM calls won't be captured as parts of an Agent Step.
if api_key := os.environ.get("OPENAI_API_KEY", None):
logger.info("Using OpenAI API.")
from llama_index.llms.openai import OpenAI
Settings.llm = OpenAI(
model="gpt-4o-mini",
api_key=api_key,
is_function_calling_model=True,
is_chat_model=True,
)
elif api_key := os.environ.get("TOGETHER_AI_API_KEY", None):
logger.info("Using Together AI API.")
from llama_index.llms.openai_like import OpenAILike
Settings.llm = OpenAILike(
model="meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo",
api_base="https://api.together.xyz/v1",
api_key=api_key,
is_function_calling_model=True,
is_chat_model=True,
)
else:
logger.info("Using Ollama's OpenAI-compatible API.")
from llama_index.llms.openai_like import OpenAILike
Settings.llm = OpenAILike(
model="llama3.1",
api_base="http://localhost:11434/v1",
api_key="ollama",
is_function_calling_model=True,
is_chat_model=True,
)
Settings.embed_model = OllamaEmbedding(
# https://ollama.com/library/nomic-embed-text
model_name="nomic-embed-text",
# Uncomment the following line to use the LLM server running on my gaming PC.
# base_url="http://10.147.20.237:11434",
)
# ============= End of the code block for wiring on to models. =============
if api_key := os.environ.get("TAVILY_API_KEY", None):
# Manage your API keys here: https://app.tavily.com/home
logger.info(
"Thanks for providing a Tavily API key. This AI agent will be able to use search the internet."
)
tavily_tool = TavilyToolSpec(
api_key=api_key,
).to_tool_list()
else:
tavily_tool = []
all_tools = tavily_tool + [
FunctionTool.from_defaults(
ToolForSuggestingChoices().suggest_choices,
),
FunctionTool.from_defaults(
ToolForConsultingTheModule().consult_the_game_module,
),
FunctionTool.from_defaults(
roll_a_dice,
),
FunctionTool.from_defaults(
roll_a_skill,
),
FunctionTool.from_defaults(
illustrate_a_scene,
),
tool_for_creating_character,
]
# Override the default system prompt for ReAct chats.
with open("prompts/system_prompt.md") as f:
MY_SYSTEM_PROMPT = f.read()
my_system_prompt = MY_SYSTEM_PROMPT.replace(
# TODO: Use `PromptTemplate.partial_format`. Today, it's not working.
"{allowance}",
str(max_action_steps),
)
return all_tools, my_system_prompt
all_tools, my_system_prompt = set_up_llama_index()
@cl.set_starters
async def set_starters():
return [
cl.Starter(
label="Roll a 7-faced dice. Outcome?",
message="Roll a 7-faced dice just for fun. What's the outcome?",
icon="/public/avatars/roll_a_dice.png",
),
cl.Starter(
label="I'm stuck in a cave. What skills to use?",
message="I'm stuck in a dark cave. What can I do?",
icon="/public/avatars/suggest_choices.png",
),
cl.Starter(
label="Create a character for me.",
message='Can you generate a character for me? Let\'s call him "Don Joe". Describe what kind of guy he is.',
icon="/public/avatars/create_character.png",
),
cl.Starter(
label="What's the story background?",
message="According to the game module, what's the background of the story?",
icon="/public/avatars/consult_the_game_module.png",
),
]
@cl.on_chat_start
async def factory():
# Each chat session should have his own agent runner, because each chat session has different chat histories.
key = cl.user_session.get("id")
chat_memory = ChatMemoryBuffer.from_defaults(
chat_store=chat_store,
chat_store_key=key,
)
agent_runner = OpenAIAgent.from_tools(
system_prompt=my_system_prompt,
tools=all_tools,
verbose=True,
# x2: An observation step also takes as an iteration.
# +1: The final output reasoning step needs to take a spot.
memory=chat_memory,
)
cl.user_session.set(
"agent",
agent_runner,
)
@cl.on_chat_end
async def cleanup():
pass
@cl.on_message
async def handle_message_from_user(message: cl.Message):
agent: AgentRunner = cl.user_session.get("agent")
# The Chainlit doc recommends using `await cl.make_async(agent.chat)(message.content)` instead:
# > The make_async function takes a synchronous function (for instance a LangChain agent) and returns an
# > asynchronous function that will run the original function in a separate thread. This is useful to run
# > long running synchronous tasks without blocking the event loop.
# (https://docs.chainlit.io/api-reference/make-async#make-async)
# I thought we can just use `agent.achat` directly, but it would cause `<ContextVar name='chainlit' at 0x...>`.
# TODO: streaming seems broken. Why?
response = await cl.make_async(agent.chat)(message.content)
response_message = cl.Message(content="")
response_message.content = response.response
await response_message.send()