answerQuestions.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import pandas as pd
  2. import chromadb
  3. import os
  4. import erniebot
  5. erniebot.api_type = "aistudio"
  6. erniebot.access_token = "ff1531c8c0f429f92adbc2eaed2e23bfb5349e0f"
  7. chroma_client = chromadb.PersistentClient(path="data/chroma")
  8. vectordb = chroma_client.get_collection(name="collection")
  9. def generateStory(question):
  10. query = [question, ]
  11. response = erniebot.Embedding.create(
  12. input=query,
  13. model='ernie-text-embedding',
  14. )
  15. query_embeddings = response.data[0]['embedding']
  16. print(query_embeddings)
  17. results = vectordb.query(
  18. query_embeddings=query_embeddings,
  19. n_results=1 # top_k
  20. )
  21. search_result = {'metadatas': results['metadatas'][0], 'documents': results['documents'][0]}
  22. prompt = f"你是一个擅长给小朋友讲故事的小助手,请根据自己的知识和检索到的内容,对小朋友的疑问进行解答。注意,由于小朋友的年龄较小,问题解答需要用故事的形式。尽量使故事生动、有趣、富有哲理。 \n 用户问题: {question} \n\n 搜索结果:\n {search_result}"
  23. print(prompt)
  24. messages = [
  25. {"role": "user", "content": prompt}
  26. ]
  27. response = erniebot.ChatCompletion.create(
  28. model="ernie-4.0",
  29. messages=messages
  30. )
  31. story = response.get_result()
  32. return story
  33. question = "什么是克隆羊"
  34. print(generateStory(question))