faq.mdx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ---
  2. title: "FAQ"
  3. description: "Find answers to common questions about Agency Swarm."
  4. icon: "question"
  5. ---
  6. <AccordionGroup defaultOpen={true}>
  7. <Accordion title="How do I set my OpenAI API key in my project?" icon="key">
  8. Set your API key in your code:
  9. ```python
  10. from agency_swarm import set_openai_key
  11. set_openai_key("YOUR_API_KEY")
  12. ```
  13. Or use a `.env` file:
  14. ```env
  15. OPENAI_API_KEY=sk-1234...
  16. ```
  17. Then load it with:
  18. ```python
  19. from dotenv import load_dotenv
  20. load_dotenv()
  21. ```
  22. </Accordion>
  23. <Accordion title="Can I use open source models with Agency Swarm?" icon="code-fork">
  24. Yes—you can use third-party models for simple, non–mission-critical tasks (usually one or two tools per agent). See [Third-Party Models](/additional-features/third-party-models) for more information. Keep in mind that many third-party models currently struggle with function calling.
  25. </Accordion>
  26. <Accordion title="How do I save and continue conversations?" icon="messages">
  27. To persist conversations between application restarts, implement callbacks that save and load the full message history from a local file. For example, define your callback functions:
  28. ```python
  29. import os
  30. import json
  31. def load_threads(chat_id: str) -> list[dict[str, TResponseInputItem]]:
  32. """Load all threads data for a specific chat session."""
  33. if os.path.exists(f"{chat_id}_threads.json"):
  34. with open(f"{chat_id}_threads.json", "r") as file:
  35. return json.load(file)
  36. return []
  37. def save_threads(thread_dict: list[dict[str, TResponseInputItem]], chat_id: str):
  38. """Save all threads data to file."""
  39. with open(f"{chat_id}_threads.json", "w") as file:
  40. json.dump(thread_dict, file)
  41. # Then, pass these callbacks during your agency initialization to resume conversations:
  42. from agency_swarm import Agency
  43. agency = Agency(
  44. agent,
  45. load_threads_callback=lambda: load_threads(chat_id),
  46. save_threads_callback=lambda thread_dict: save_threads(thread_dict, chat_id),
  47. )
  48. ```
  49. This setup preserves your conversation context between runs.
  50. </Accordion>
  51. <Accordion title="How do I manage multiple users with Agency Swarm?" icon="users">
  52. To support multiple users/chats, you need to load and save thread IDs in your database accordingly. Each chat/user should have unique thread IDs. Ensure to check out our [Deployment to Production](/additional-features/deployment-to-production) guide for more information.
  53. </Accordion>
  54. <Accordion title="How can I transfer data between tools and agents?" icon="upload">
  55. There are two ways to transfer data between tools and agents:
  56. 1. Use agency context inside your tools. Read more: [Agency Context](/additional-features/agency-context)
  57. 2. Create a tool (or modify an existing one) that uploads files to storage and outputs the file ID. This file ID can then be used by other tools or agents.
  58. </Accordion>
  59. <Accordion title="Why is the CodeInterpreter tool automatically added?" icon="code">
  60. When file types like `.json`, `.docx`, or `.pptx` are uploaded, CodeInterpreter is auto-added to process them. To change the agent's behavior, update its instructions or create a custom file-handling tool.
  61. </Accordion>
  62. <Accordion title="How can I serve an Agency as an API using FastAPI?" icon="book">
  63. Embed your agency within a FastAPI endpoint:
  64. ```python
  65. from fastapi import FastAPI
  66. from uuid import uuid4
  67. app = FastAPI()
  68. @app.post("/chat")
  69. async def chat(user_request: UserRequest):
  70. chat_id = user_request.chat_id or str(uuid4())
  71. agency = Agency(
  72. agent,
  73. load_threads_callback=lambda: load_threads(chat_id),
  74. save_threads_callback=lambda thread_dict: save_threads(thread_dict, chat_id)
  75. )
  76. response = await agency.get_response(user_request.message)
  77. return {"chat_id": chat_id, "response": response.final_output}
  78. # Or use the built-in FastAPI integration
  79. agency.run_fastapi(host="0.0.0.0", port=8000)
  80. ```
  81. </Accordion>
  82. <Accordion title="How do I deploy my agency to production?" icon="rocket">
  83. Build a dedicated API backend (FastAPI is recommended) that manages authentication and persists thread state using callbacks. For more details, refer to our [Deployment to Production](/additional-features/deployment-to-production) guide.
  84. </Accordion>
  85. </AccordionGroup>
  86. ## Getting Support
  87. <CardGroup cols={2}>
  88. <Card title="Community Support" icon="discord" href="https://discord.gg/cw2xBaWfFM">
  89. Join our Discord community for quick help and discussions.
  90. </Card>
  91. <Card title="Professional Services" icon="briefcase" href="https://agents.vrsen.ai/">
  92. Get professional help with our Agents-as-a-Service subscription.
  93. </Card>
  94. </CardGroup>