Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_openai import ChatOpenAI
|
| 2 |
+
from langchain.chains import LLMChain
|
| 3 |
+
from promtps import coding_hints_prompt_template
|
| 4 |
+
from langchain.memory.buffer import ConversationBufferMemory
|
| 5 |
+
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
import chainlit as cl
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@cl.on_chat_start
|
| 14 |
+
def query_llm():
|
| 15 |
+
llm = ChatOpenAI(model="gpt-4o",
|
| 16 |
+
temperature=0.3)
|
| 17 |
+
conversation_memory = ConversationBufferMemory(memory_key="chat_history",
|
| 18 |
+
return_messages=True,
|
| 19 |
+
)
|
| 20 |
+
llm_chain = LLMChain(llm=llm, prompt=coding_hints_prompt_template, memory=conversation_memory)
|
| 21 |
+
cl.user_session.set("llm_chain", llm_chain)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@cl.on_message
|
| 25 |
+
async def query_llm(message: cl.Message):
|
| 26 |
+
llm_chain = cl.user_session.get("llm_chain")
|
| 27 |
+
response = await llm_chain.acall(message.content,
|
| 28 |
+
callbacks=[
|
| 29 |
+
cl.AsyncLangchainCallbackHandler()])
|
| 30 |
+
|
| 31 |
+
await cl.Message(response["text"]).send()
|
| 32 |
+
|
| 33 |
+
example_input = """Implement Selection Sort
|
| 34 |
+
Given a list of numbers, sort it using the Selection Sort algorithm.
|
| 35 |
+
|
| 36 |
+
Example
|
| 37 |
+
{
|
| 38 |
+
"arr": [5, 8, 3, 9, 4, 1, 7]
|
| 39 |
+
}
|
| 40 |
+
Output:
|
| 41 |
+
|
| 42 |
+
[1, 3, 4, 5, 7, 8, 9]
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
Constraints:
|
| 46 |
+
1 <= length of the given list <= 4 * 10^3
|
| 47 |
+
-10^9 <= number in the list <= 10^9"""
|