Spaces:
Runtime error
Runtime error
File size: 2,996 Bytes
6627dda |
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 |
from openai import OpenAI
from .tools import tools
from .utils import system_prompt, handle_tool_calls
# okay now we have app file I got it from smola agent its a taste file I have agent use that agent and add that agent in app in such a way that api come with questions and this agent give answers check how app file is made you will undertand dont change structure and way and logis ogf app file
# Initialize OpenAI client
client = OpenAI(
api_key="sk-60c49cf5d82a4d4bb0a6c111eeafe941",
base_url="https://api.deepseek.com"
)
def chat_with_agent(user_message):
"""Main function to chat with the agent - gives short, concise responses"""
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_message}
]
while True:
# Get response from LLM with token limits
response = client.chat.completions.create(
model="deepseek-chat",
messages=messages,
tools=tools,
)
# Add the assistant's response to messages
messages.append(response.choices[0].message)
# Handle tool calls if any
tool_results = handle_tool_calls(response)
if tool_results:
# Add tool results to messages
messages.extend(tool_results)
# Add a reminder for the final response to be short
messages.append({
"role": "system",
"content": "Remember: Give a SHORT summary (2-3 lines max) of the tool results. Focus on key points only."
})
# Continue the conversation (the LLM will respond to the tool results)
continue
else:
# No tool calls, return the final response
return response.choices[0].message.content
# def interactive_chat():
# """Interactive chat interface with short responses"""
# print("🤖 Welcome to the AI Agent!")
# print("I'll give you short, concise answers.")
# print("Type 'quit' to exit.")
# print("-" * 50)
# while True:
# user_input = input("\n👤 You: ").strip()
# if user_input.lower() in ['quit', 'exit', 'bye', 'q']:
# print("🤖 Goodbye!")
# break
# if not user_input:
# continue
# try:
# print("🤖 Thinking...")
# response = chat_with_agent(user_input)
# print(f"🤖 Assistant: {response}")
# except Exception as e:
# print(f"🤖 Error: {str(e)}")
# Example usage
# if __name__ == "__main__":
# # Interactive mode
# interactive_chat()
# Uncomment for testing with specific questions
# user_question = "What's the weather like in Mumbai?"
# print(f"User: {user_question}")
# answer = chat_with_agent(user_question)
# print(f"Assistant: {answer}")
print(chat_with_agent("tech news today")) |