Skip to content

Instantly share code, notes, and snippets.

@astoeckl
Created December 12, 2021 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astoeckl/cc0d746db124c911e90757368332dcf5 to your computer and use it in GitHub Desktop.
Save astoeckl/cc0d746db124c911e90757368332dcf5 to your computer and use it in GitHub Desktop.
import openai
openai.api_key = "XXX-YOURKEY"
doc_per_cluster = 3
for i in range(no_clusters):
print(f"Cluster {i} Topic:", end=" ")
docs = "\n".join(df[df.Cluster == i].Text.map(lambda x: x[:1000]).sample(doc_per_cluster, random_state=42).values)
response = openai.Completion.create(
engine="davinci-instruct-beta-v3",
prompt=f"What do the following documents have in common?\n\nDocuments:\n\"\"\"\n{docs}\n\"\"\"\n\nTopic:",
temperature=0,
max_tokens=64,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
print(response["choices"][0]["text"].replace('\n',''))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment