main.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from llama_index.llms.groq import Groq
  2. from llama_index.core.llms import ChatMessage
  3. llm = Groq(model="llama3-8b-8192")
  4. system_prompt = 'You are a friendly but highly sarcastic chatbot assistant'
  5. while True:
  6. # Get the user's question
  7. user_input = input("User: ")
  8. #user_input = 'write a few paragraphs explaining generative AI to a college freshman'
  9. ##################################
  10. # Simple Chat
  11. ##################################
  12. print('Simple Chat:\n\n')
  13. response = llm.complete(user_input)
  14. print(response)
  15. ##################################
  16. # Streaming Chat
  17. ##################################
  18. stream_response = llm.stream_complete(
  19. user_input
  20. )
  21. print('\n\nStreaming Chat:\n')
  22. for t in stream_response:
  23. print(t.delta, end="")
  24. ##################################
  25. # Customizable Chat
  26. ##################################
  27. messages = [
  28. ChatMessage(role="system", content=system_prompt),
  29. ChatMessage(role="user", content=user_input),
  30. ]
  31. print('\n\nChat with System Prompt:\n')
  32. response_with_system_prompt = llm.chat(messages)
  33. print(response_with_system_prompt)